1

This is a sample of my dataframe. I have three years, three states, and six roles. I would like to subset my dataframe so that I have a new dataframe for every combination of year and state for all six roles.

> A[1:10,c(1,4,64:69)]
   Year State Role1 Role2 Role3 Role4 Role5 Role6
1  2010   1     1     0     1     1     0     0
2  2012   3     1     0     0     1     0     0
3  2010   2     1     0     0     1     0     0
4  2011   1     1     0     0     1     0     0
5  2011   3     0     1     0     1     0     0
6  2010   2     1     0     0     1     0     0
7  2010   2     0     1     0     1     0     0
8  2012   2     0     1     0     1     0     0
9  2012   2     0     0     0     0     0     1
10 2011   1     0     0     1     0     0     0

I could do something like this and repeat it for all combinations:

2010_1_Role1 <- subset(A, Year=="2010" & State=="1" & Role1=="1")
2011_1_Role1 <- subset(A, Year=="2011" & State=="1" & Role1=="1")
2012_1_Role1 <- subset(A, Year=="2012" & State=="1" & Role1=="1")

...I don't have a lot of experience writing loops, but I figured there's probably a faster way to do it writing a for loop! Any suggestions? Thank you!

Juli
  • 37
  • 3

1 Answers1

1

We can use split to give a list of data.frame for all the unique combinations of the groups

lst1 <- split(A, A[c("Year", "State", "Role1")], drop = TRUE)

and then do all the transformations within the list using lapply/sapply

It is not recommended to have multiple objects in the global environment. If we need,

list2env(lst1, .GlobalEnv)

after changing the names of the list with the preferred object names.


It is not clear why we need to subset. If this is to execute some code over each group, then a group_by operation is preferrable

library(dplyr)
A %>%
  group_b(Year, State, Role1) %>%
  mutate(Colname = yourfun(Colname))
  # or for summarise
  # summarise(n = n())
akrun
  • 874,273
  • 37
  • 540
  • 662