1

I'm trying to create new objects for each item in the column "category."

rs_ <- regular_season %>% filter(user_id > 0)
unique_categories <- unique(regular_season$category)

for (i in unique_categories)
  rs_[i] <- regular_season %>% filter(category %in% unique_categories)

I've tried to create a template object "rs_" that I would iteratively add to (it's essentially the dataset with a null condition on it, just so it doesn't simply rename the dataset). Can someone show me how to better do this?

I have looked at some other questions that suggest using other things like lists but (of course without knowing too much about R) for the number of items in the column, it doesn't seem like a for loop would terrible, and I've had some trouble figuring out how to use lists anyway.

1 Answer 1

3

We can use assign

for(i in unique_categories) {
      assign(i, regular_season %>%
            filter(category %in% i)
  }

Or another option is split and list2env

list2env(split(regular_season, regular_season$category), 
        .GlobalEnv)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @akrun this is very helpful! The website won't let me accept your answer yet but I will accept it when it will allow me, in about 10min

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.