Split/subset a data frame by factors in one column [duplicate]

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.

Leave a Comment