# [R] Aggregate

David Carlson dcarlson at tamu.edu
Mon Jul 29 23:28:57 CEST 2013

```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:
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

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