[R] subset a data frame into multiple data frames

arun smartpink111 at yahoo.com
Tue Jan 28 03:51:33 CET 2014


Hi,
Try ?split()
If `dat1` is the dataset:
lst1 <- split(dat1,dat1$ID)
lst1$an1
#   ID V1      mean       SD       SE
#1 an1  5  72.21719 22.27118 9.092172
#2 an1  6 100.00000       NA       NA


lst1$an2
#   ID V1      mean       SD      SE
#3 an2  5  79.27999 25.08938 10.2427
#4 an2  6 100.00000       NA      NA


A.K.


I have a data frame with an ID column and multiple IDs with rows of 
data that correspond to them. I need to subset the data frame such that 
each ID becomes its own data frame, and the name of the data frame 
should be its ID. I have quite a few IDs so doing this one subset at a 
time would be very tedious, but I have not been able to figure out how 
to loop it. I read about using write.table in a for loop to generate 
multiple excel files, but I want these data frames to remain in R. Here 
is a small example of my data frame (called "data"). 

  ID V1      mean       SD        SE 
an1  5  72.21719 22.27118  9.092172 
an1  6 100.00000       NA        NA 
an2  5  79.27999 25.08938 10.242698 
an2  6 100.00000       NA        NA 

after subseting it, I want to be able to enter print(an1) and get 
  ID V1      mean       SD        SE 
an1  5  72.21719 22.27118  9.092172 
an1  6 100.00000       NA        NA 

similarly, if I enter print(an2) I should get 
  ID V1      mean       SD        SE 
an2  5  79.27999 25.08938 10.242698 
an2  6 100.00000       NA        NA 

I've looked around online, but I haven't been able to find the answer. 
Thanks for the help.



More information about the R-help mailing list