Thanks a lot Arun. 
I like the do.call command a lot. So easy to use and fast:-)
 
Best,Farnoosh Sheikhi


________________________________


Cc: R help <r-help@r-project.org> 
Sent: Monday, July 29, 2013 1:58 PM
Subject: Re: Aggregate




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.


________________________________


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 
	[[alternative HTML version deleted]]

