[R] Summarizing elements for a data.frame

arun smartpink111 at yahoo.com
Tue Dec 18 16:47:51 CET 2012



Hi,

You could also use ?xtabs()
dat1<-structure(list(Col1 = structure(c(1L, 3L, 2L, 1L, 3L, 3L, 1L), .Label = c("A", 
"B", "C"), class = "factor"), Col2 = structure(c(2L, 2L, 1L, 
1L, 2L, 1L, 2L), .Label = c("a", "b"), class = "factor"), Col3 = c(1, 
2, 3, 4, 5, 6, 2)), .Names = c("Col1", "Col2", "Col3"), row.names = c(NA, 
7L), class = "data.frame")
xtabs(Col3~Col1+Col2,data=dat1)
#    Col2
#Col1 a b
 # A 4 3
  # B 3 0
  # C 6 7
tapply(dat1[,3],list(dat1[,1],dat1[,2]),sum)
#  a  b
#A 4  3
#B 3 NA
#C 6  7
A.K.



----- Original Message -----
From: Christofer Bogaso <bogaso.christofer at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Tuesday, December 18, 2012 9:36 AM
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.





More information about the R-help mailing list