[R] Summarizing elements for a data.frame

John Kane jrkrideau at inbox.com
Tue Dec 18 17:44:04 CET 2012


And yet another way.

library(reshape2)
names(Dat1)  <-  c("big","small","nums")
dcast(Dat1 , big ~ small, sum)

John Kane
Kingston ON Canada


> -----Original Message-----
> From: bogaso.christofer at gmail.com
> Sent: Tue, 18 Dec 2012 20:06:42 +0530
> To: r-help at r-project.org
> Subject: [R] Summarizing elements for a data.frame
> 
> Hello again, let say we have following data:
> 
> Dat1 <- structure(list(factor.sample.LETTERS.1.3...6..replace...T.. =
> structure(c(1L,
> 3L, 2L, 1L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"),
>     factor.sample.letters.1.2...6..replace...T.. = structure(c(2L,
>     2L, 1L, 1L, 2L, 1L), .Label = c("a", "b"), class = "factor"),
>     X1.6 = 1:6), .Names =
> c("factor.sample.LETTERS.1.3...6..replace...T..",
> "factor.sample.letters.1.2...6..replace...T..", "X1.6"), row.names =
> c(NA,
> -6L), class = "data.frame")
> 
> Out of this data.frame, I want to create a Table with rows coming from
> 1st column of Dat1 and columns are coming from 2nd column of Dat1 and
> each entry will be the sum for 3rd column of Dat1, i.e. the element
> for (1,1) will be sum for all element in 3rd column corresponding to
> (A, a) and so on.
> 
> I tried with table() however could not achieve what I wanted.
> 
> Can somebody give me some pointer?
> 
> Thanks and regards,
> 
> ______________________________________________
> 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.

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!




More information about the R-help mailing list