Hello: I'm reading in a series of text files (100 files that are each 2000 rows by 6 columns). I wish to combine the columns (6) of each file (100) and get the row mean. I'd like to end up with a data.frame of 2000 rows by 6 columns. foo <- list() for(i in 1:10){ # The real data are read in from a series of numbered text files foo[[i]] <- data.frame(x1 = rnorm(100), x2 = rnorm(100), x3 rnorm(100), x4 = rnorm(100), x5 = rnorm(100), x6 rnorm(100)) } str(foo) # by hand mean.x1 <- rowMeans(cbind(foo[[1]][,1],foo[[2]][,1],foo[[3]][,1],foo[[4]][,1],foo[[5]][ ,1]), foo[[6]][,1],foo[[7]][,1],foo[[8]][,1],foo[[9]][,1 ],foo[[10]][,1])) mean.x2 <- rowMeans(cbind(foo[[1]][,2],foo[[2]][,2],foo[[3]][,2],foo[[4]][,2],foo[[5]][ ,2]), foo[[6]][,2],foo[[7]][,2],foo[[8]][,2],foo[[9]][,2 ],foo[[10]][,2])) # and so on to column 6 mean.x6 <- rowMeans(cbind(foo[[1]][,6],foo[[2]][,6],foo[[3]][,6],foo[[4]][,6],foo[[5]][ ,6]), foo[[6]][,6],foo[[7]][,6],foo[[8]][,6],foo[[9]][,6 ],foo[[10]][,6])) I've implemented this with nested loops that create temporary variables and calc the mean, but the approach is clunky. E.g., # nested loops for(i in 1:ncol(foo[[1]])){ for(j in 1:length(foo)){ # etc ... } } Is there a way to build a better mouse trap? TIA, Andy Thanks, Andy
Andy Bunn wrote:> Hello: I'm reading in a series of text files (100 files that are each 2000 > rows by 6 columns). I wish to combine the columns (6) of each file (100) and > get the row mean. I'd like to end up with a data.frame of 2000 rows by 6 > columns. > > foo <- list() > for(i in 1:10){ > # The real data are read in from a series of numbered text files > foo[[i]] <- data.frame(x1 = rnorm(100), x2 = rnorm(100), x3 > rnorm(100), > x4 = rnorm(100), x5 = rnorm(100), x6 > rnorm(100)) > > } > > str(foo) > # by hand > mean.x1 <- > rowMeans(cbind(foo[[1]][,1],foo[[2]][,1],foo[[3]][,1],foo[[4]][,1],foo[[5]][ > ,1]), > foo[[6]][,1],foo[[7]][,1],foo[[8]][,1],foo[[9]][,1 > ],foo[[10]][,1])) > mean.x2 <- > rowMeans(cbind(foo[[1]][,2],foo[[2]][,2],foo[[3]][,2],foo[[4]][,2],foo[[5]][ > ,2]), > foo[[6]][,2],foo[[7]][,2],foo[[8]][,2],foo[[9]][,2 > ],foo[[10]][,2])) > # and so on to column 6 > mean.x6 <- > rowMeans(cbind(foo[[1]][,6],foo[[2]][,6],foo[[3]][,6],foo[[4]][,6],foo[[5]][ > ,6]), > foo[[6]][,6],foo[[7]][,6],foo[[8]][,6],foo[[9]][,6 > ],foo[[10]][,6])) > > > I've implemented this with nested loops that create temporary variables and > calc the mean, but the approach is clunky. E.g., > > # nested loops > for(i in 1:ncol(foo[[1]])){ > for(j in 1:length(foo)){ > # etc ... > } > } > > Is there a way to build a better mouse trap? > > TIA, AndyI don't know of a way of getting around at least one for loop, but the following might be want you need: r <- matrix(, NROW(foo[[1]]), length(foo)) for(i in 1:NCOL(foo[[1]])) r[, i] <- rowMeans(do.call("cbind", lapply(foo, "[", i))) dim(r) This assumes each element of foo has identical dimensions. Otherwise you'll get an error. HTH, --sundar
I think about half of my question in R can be solved with a judicious do.call. Thanks, Andy
Justone more comment in addition to Sundar's solution: If these are all numeric matrices, I would read them into R as such, instead of data frames. Actually, I would read them all into a 3-dimensional array (2000 x 6 x # of files). Assuming you have such an array, then you can do something like:> ## get your example into an array: need the abind package. > bar <- do.call("abind", c(lapply(foo, as.matrix), along=3)) > str(bar)num [1:100, 1:6, 1:10] -0.78981 0.31939 -0.00819 1.59346 1.20498 ... - attr(*, "dimnames")=List of 3 ..$ : chr [1:100] "1" "2" "3" "4" ... ..$ : chr [1:6] "x1" "x2" "x3" "x4" ... ..$ : NULL> str(m <- rowMeans(bar, dims=2))num [1:100, 1:6] -0.4401 0.5463 -0.0572 -0.1314 0.5177 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:100] "1" "2" "3" "4" ... ..$ : chr [1:6] "x1" "x2" "x3" "x4" ... Andy> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Andy Bunn > Sent: Tuesday, July 19, 2005 3:59 PM > To: Sundar Dorai-Raj > Cc: R-Help > Subject: Re: [R] extracting row means from a list > > > I think about half of my question in R can be solved with a judicious > do.call. > > Thanks, Andy > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >