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 Christofer, Check ?tapply, ?aggregate and ?ave. Here is a way of doing in using the first function: tapply(Dat1[, 3], list(Dat1[,1], Dat1[,2]), sum, na.rm = TRUE) HTH, Jorge.- On Wed, Dec 19, 2012 at 1:36 AM, Christofer Bogaso <> wrote:> 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") >[[alternative HTML version deleted]]
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> -----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!
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.
> 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....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}}
On Wed, Dec 19, 2012 at 9:47 AM, ?hagen Patrik <Patrik.Ohagen at mpa.se> wrote:> 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.... >How huge is "huge"? What is 'the ordinary "read syntax"'? Have you really waited forever? Barry