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?
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]]
Or just table(dat1$ID, dat1$Group1) # 0 1 # 1 2 2 # 2 1 3 # 5 1 3 Or xtabs(~ID+Group1, dat1) # Group1 # ID 0 1 # 1 2 2 # 2 1 3 # 5 1 3 Or with labeling dat1$Group1 <- factor(dat1$Group1, labels=c("No", "Yes")) xtabs(~ID+Group1, dat1) # Group1 # ID No Yes # 1 2 2 # 2 1 3 # 5 1 3 ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of arun Sent: Monday, July 29, 2013 3:58 PM To: farnoosh sheikhi Cc: R help Subject: Re: [R] 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)-s um(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> 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? ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.