29

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
titi
  • 609
  • 2
  • 7
  • 9
  • 8
    If you think about the `split` function, why don't you try it ? – juba Oct 11 '13 at 20:53
  • Hi! Please search thoroughly for an answer before asking your question. Please also read [this](http://stackoverflow.com/help/on-topic): "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results.". That said, welcome to SO! Cheers. – Henrik Oct 11 '13 at 21:05
  • 7
    Actually I tried split function, but what i got is a list. I am not sure whether I am on the right track or maybe there are some functions that I don't know, that's why I asked. Please don't consider me as not putting any effort in it and just throw out the question at the beginning. Plus, my actually data are way more complicated than the example, it is relative hard to check whether i did it correct. I appreciate both of your comments but a little bit more tolerance to beginners would be appreciated. – titi Oct 11 '13 at 21:20

1 Answers1

56

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184