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!