[R] Aggregate

arun smartpink111 at yahoo.com
Mon Jul 29 22:58:19 CEST 2013



Hi,
You could try:
dat1<- read.table(text="
ID     Group1
1     1
1     0
1     1
1     0
2     1
2     1
2     0
2     1
5     1
5     1
5     1
5     0
",sep="",header=TRUE)

library(plyr)
res<- ddply(dat1,.(ID),summarize,yes=sum(Group1),no=length(Group1)-sum(Group1))
res
#  ID yes no
#1  1   2  2
#2  2   3  1 #need to check
#3  5   3  1 ###


#or
do.call(rbind,by(dat1$Group1,dat1$ID,table))
#  0 1
#1 2 2
#2 1 3
#5 1 3

#or
 do.call(rbind,with(dat1,tapply(Group1,ID,FUN=table)))
#  0 1
#1 2 2
#2 1 3
#5 1 3



A.K.


________________________________
From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Monday, July 29, 2013 4:37 PM
Subject: Aggregate



Hi Arun,

I have a question about aggregation in R.
I have the following data set:

ID Group1 
1 1 
1 0 
1 1 
1 0 
2 1 
2 1 
2 0 
2 1 
5 1 
5 1 
5 1 
5 0 

I want to aggregate the data for each ID to get number of zeros and number of ones. something like the following data sets:
     ID     yes no 
1 2 2 
2 3 0 
5 3 0 



I though I can put the number of ones as YES and the number of Zeroes as NO.
Thanks a lot.
Best,Farnoosh Sheikhi 



More information about the R-help mailing list