1

Hello I have created a forloop to split my data with respect to a certain row as so:

for(i in 1:(nrow(df)))

{

team_[[i]] <- df %>% filter(team == i)

}

R doesn't like this, as it says team_ not found. the code does run if I include a list as such

team_ <- list()

for(i in 1:(nrow(df)))

{

team_[[i]] <- df %>% filter(team == i)

}

This works... However, I am given a list with thousands of empty items and just a few that contain my filtered data sets.

is there a simpler way to create the data sets without this list function?

thank you

0

2 Answers 2

3

A simpler option is split from base R which would be faster than using == to subset in a loop

team_ <- split(df, df$team)

If we want to do some operations for each row, in tidyverse, it can be done with rowwise

library(dplyr)
df %>%
    rowwise %>%
    ... step of operations ...

or with group_by

df %>%
   group_by(team) %>%
   ...
Sign up to request clarification or add additional context in comments.

4 Comments

Wow... I cannot believe you have answered that with 1 line. Thank you so much.
How do we then extract the data frames from the list?
@T.Omalley you can use regular extraction methods i.e. [[ - team_[[1]] or ` wiith the names team_[["teamname1"]] or with $ team_$teamname1
I will be doing so, I have to wait a certain amount of time before I can accept it
2

The methods akrun suggests are much better than a loop, but you should understand why this isn't working. Remember for(i in 1:nrow(df)) will give you one list item for i = 1, i = 2, etc, right up until i = nrow(df), which is several thousand by the sounds of thing. If you don't have any rows where team is 1, you will get an empty data frame as the first item, and the same will be true for every other value of i that isn't represented.

A loop like this would work:

for(i in unique(df$team)) team_[[i]] <- df %>% filter(team == i)

But I would stick to a non-looping method as described by akrun.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.