Skip to content

Summarizing elements for a data.frame

7 messages · Christofer Bogaso, Jorge I Velez, arun +4 more

#
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,
#
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.
#
And yet another way.

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

John Kane
Kingston ON Canada
____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!
#
Dear List,

Are there any tricks or wise suggestions on how to import huge comma separated files into R? We have used the ordinary "read syntax" but it takes forever....

Thank you in advance!

Cheers, Patrik
#
The sqldf package may be of help to you. 
Regards
S?ren


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of ?hagen Patrik
Sent: 19. december 2012 10:48
To: R hELP
Subject: [R] Reading extremly large comma separated files?

Dear List,

Are there any tricks or wise suggestions on how to import huge comma separated files into R? We have used the ordinary "read syntax" but it takes forever....

Thank you in advance!

Cheers, Patrik

______________________________________________
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.
#
One potential time- and memory saver is to specify the class of each column using colClasses in read.table.

For a large data set (800 000 rows) on Windows I also found it quicker and more reliable to import the data into a MySQL database and then use ODBC  to read the data into R than to use read.table on the raw data - but that was in part because I hada bit of trouble with the file encoding and MySQL/ODBC sidestepped the issue. 

S Ellison 

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}