Split/subset a data frame by factors in one column [duplicate]
My data is like this (for example):
ID Rate State
1 24 AL
2 35 MN
3 46 FL
4 34 AL
5 78 MN
6 99 FL
Data:
structure(list(ID = 1:6, Rate = c(24L, 35L, 46L, 34L, 78L, 99L),
State = structure(c(1L, 3L, 2L, 1L, 3L, 2L),
.Label = c("AL","FL", "MN"),
class = "factor")),
.Names = c("ID", "Rate", "State"),
class = "data.frame", row.names = c(NA, -6L))
I want to split the data by state and I want to get 3 data sets like below:
data set 1
ID Rate State
1 24 AL
4 34 AL
data set 2
ID Rate State
2 35 MN
5 78 MN
data set 3
ID Rate State
3 46 FL
6 99 FL
What function I should use?
I was thinking about split or subset function, but still have no clue yet.
Solution 1:
We could use split
:
mylist <- split(df, df$State)
mylist
$AL
ID Rate State
1 1 24 AL
4 4 34 AL
$FL
ID Rate State
3 3 46 FL
6 6 99 FL
$MN
ID Rate State
2 2 35 MN
5 5 78 MN
To access elements number:
mylist[[1]]
or by name:
mylist$AL
ID Rate State
1 1 24 AL
4 4 34 AL
?split
Description
split divides the data in the vector x into the groups defined by f. The replacement forms replace values corresponding to such a division. unsplit reverses the effect of split.