[R] Data manipulation

David L Carlson dcarlson at tamu.edu
Fri Mar 15 15:45:00 CET 2013


I was too quick on the Send button. Xtabs produces a table. If you want a data.frame, it would be data.frame(xtabs(Count~Class+X, D)):

# Match John's summary table and generate Counts
> set.seed(42)
> Count <- sample(1:50, 23)
> Class <- c(rep(1, 4), rep(2, 7), 3, rep(1, 3), rep(3, 4), rep(3, 4))
> X <- c(rep(.1, 12), rep(.2, 7), rep(.3, 4))
> D <- data.frame(Class=factor(Class), X=factor(X), Count)
> table(D$Class, D$X)
   
    0.1 0.2 0.3
  1   4   3   0
  2   7   0   0
  3   1   4   4

# Create the table/data.frame
> D.table <- xtabs(Count~Class+X)
> D.table
     X
Class 0.1 0.2 0.3
    1 150  63   0
    2 169   0   0
    3  41  98 114
> D.df <- data.frame(D.table)
> D.df
  Class   X Freq
1     1 0.1  150
2     2 0.1  169
3     3 0.1   41
4     1 0.2   63
5     2 0.2    0
6     3 0.2   98
7     1 0.3    0
8     2 0.3    0
9     3 0.3  114

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of David L Carlson
> Sent: Friday, March 15, 2013 9:23 AM
> To: 'IOANNA'; 'John Kane'; 'Blaser Nello'; r-help at r-project.org
> Subject: Re: [R] Data manipulation
> 
> Wouldn't this do the same thing?
> 
> xtabs(Count~Class+X, D)
> 
> ----------------------------------------------
> David L Carlson
> Associate Professor of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
> 
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of IOANNA
> > Sent: Friday, March 15, 2013 8:51 AM
> > To: 'John Kane'; 'Blaser Nello'; r-help at r-project.org
> > Subject: Re: [R] Data manipulation
> >
> > Thanks a lot!
> >
> > -----Original Message-----
> > From: John Kane [mailto:jrkrideau at inbox.com]
> > Sent: 15 March 2013 13:41
> > To: Blaser Nello; IOANNA; r-help at r-project.org
> > Subject: Re: [R] Data manipulation
> >
> > Nice. That does look like it. IOANNA?
> >
> > John Kane
> > Kingston ON Canada
> >
> >
> > > -----Original Message-----
> > > From: nblaser at ispm.unibe.ch
> > > Sent: Fri, 15 Mar 2013 14:27:03 +0100
> > > To: ii54250 at msn.com, r-help at r-project.org
> > > Subject: Re: [R] Data manipulation
> > >
> > > Is this what you want to do?
> > >
> > > D2 <- expand.grid(Class=unique(D$Class), X=unique(D$X))
> > > D2 <- merge(D2, D, all=TRUE)
> > > D2$Count[is.na(D2$Count)] <- 0
> > >
> > > W <- aggregate(D2$Count, list(D2$Class, D2$X), "sum") W
> > >
> > > Best,
> > > Nello
> > >
> > >
> > > -----Original Message-----
> > > From: r-help-bounces at r-project.org
> > > [mailto:r-help-bounces at r-project.org]
> > > On Behalf Of IOANNA
> > > Sent: Freitag, 15. März 2013 13:41
> > > To: r-help at r-project.org
> > > Subject: [R] Data manipulation
> > >
> > > Hello all,
> > >
> > >
> > >
> > > I would appreciate your thoughts on a seemingly simple problem. I
> > have
> > > a database, where each row represent a single record. I want to
> > > aggregate this database so I use the aggregate command :
> > >
> > >
> > >
> > > D<-read.csv("C:\\Users\\test.csv")
> > >
> > >
> > >
> > > attach(D)
> > >
> > >
> > >
> > > by1<-factor(Class)
> > >
> > > by2<-factor(X)
> > >
> > > W<-aggregate(x=Count,by=list(by1,by2),FUN="sum")
> > >
> > >
> > >
> > > The results I get following the form:
> > >
> > >
> > >
> > > >W
> > >
> > >   Group.1 Group.2 x
> > >
> > > 1       1     0.1 4
> > >
> > > 2       2     0.1 7
> > >
> > > 3       3     0.1 1
> > >
> > > 4       1     0.2 3
> > >
> > > 5       3     0.2 4
> > >
> > > 6       3     0.3 4
> > >
> > >
> > >
> > >
> > >
> > > However, what I really want is an aggregation which includes the
> zero
> > > values, i.e.:
> > >
> > >
> > >
> > > >W
> > >
> > >   Group.1 Group.2 x
> > >
> > > 1       1     0.1 4
> > >
> > > 2       2     0.1 7
> > >
> > > 3       3     0.1 1
> > >
> > > 4       1     0.2 3
> > >
> > >         2     0.2 0
> > >
> > > 5       3     0.2 4
> > >
> > > 1        0.3 0
> > >
> > > 2        0.3 0
> > >
> > > 6       3     0.3 4
> > >
> > >
> > >
> > >
> > >
> > > How can I achieve what I want?
> > >
> > >
> > >
> > > Best regards,
> > >
> > > Ioanna
> > >
> > > ______________________________________________
> > > 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.
> >
> > ____________________________________________________________
> > GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at
> >
> > webmails
> >
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list