Reputation: 11
Suppose I have a data frame with 7 variables. I want to subset the data frame based on the contents of one column automatically. The column is Department and there are 17 different values. I would like R to look at the column "Dept" and create a new data frame for each Dept containing all other rows. This would be the equivalent of "Split Worksheet" in Minitab. For now, I have to run the subset command 17 times to create a data frame for each. Can R do this automatically based on the column content?
Best and thanks!
Upvotes: 1
Views: 2624
Reputation: 7475
out<-split(df,df$Dept)
out[[1]]
# etc to access dataframes
or
out$Dept1
to give a concrete example
df<-data.frame(Dept=c('a','a','b','b','c','d','d'),acs=c(111,112,222,223,333,444,445))
out<-split(df,df$Dept)
out
> out
$a
Dept acs
1 a 111
2 a 112
$b
Dept acs
3 b 222
4 b 223
$c
Dept acs
5 c 333
$d
Dept acs
6 d 444
7 d 445
dept.names<-names(out)
> dept.names[1]
[1] "a"
> out[[dept.names[1]]] # dataframe for department 1
Dept acs
1 a 111
2 a 112
> out[[dept.names[2]]] # dataframe for department 2
Dept acs
3 b 222
4 b 223
> is.data.frame(out[[dept.names[2]]])
[1] TRUE
Upvotes: 5