[R] selecting certain rows from data frame

arun smartpink111 at yahoo.com
Fri May 3 22:44:21 CEST 2013



Hi,
You can use ?split()
 lst1<-split(DF,DF$ID)
lst1[1:2]
#$`1`
#  ID  drugs month
#1  1 drug x     1
#4  1 drug x     1
#5  1 drug y     2
#6  1 drug z     3
#
#$`2`
 # ID  drugs month
#2  2 drug y     2
#7  2 drug x     1

mean(sapply(lst1,nrow))
#[1] 2.4
#or
library(plyr)
 mean(ddply(DF,.(ID),nrow)[,2])
#[1] 2.4
#or
mean(with(DF,tapply(ID,ID,FUN=length)))
#[1] 2.4
A.K.




________________________________
From: Sarah Jo Sinnott <105405670 at umail.ucc.ie>
To: arun <smartpink111 at yahoo.com> 
Sent: Friday, May 3, 2013 4:35 PM
Subject: Re: selecting certain rows from data frame



Yes - but if I can count the number of rows for each ID, this equates to number of drugs per each ID. So that way I can get a mean #rows(drugs). 

e.g., 

ID 1 = 4 rows (approx=4drugs)
ID2= 2 rows
ID 3 = 3 rows
ID 4 = 2 rows
ID 5 = 1 row

12 rows/5people = 2.4rows/person

that is 2.4 drugs per person. 

Do you think it is possible to isolate the number of rows per unique ID? It would be great if you could! I'v etried reorganising my data into wide format - but it doesn't work very well, so I'm left with his option really!

Thank you for you help thus far



More information about the R-help mailing list