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

跟風遠走 提交于 2019-11-26 11:01:59

问题


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.


回答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.



来源:https://stackoverflow.com/questions/19327020/split-subset-a-data-frame-by-factors-in-one-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!