Dear useRs, I have a list, each entry of which is a matrix of constant dimensions. Is there a good way (i.e., not using a for loop) to apply a mean to each matrix entry *across list entries*? Example: foo <- list(rbind(c(1,2,3),c(4,5,6)),rbind(c(7,8,9),c(10,11,12))) some.sort.of.apply(foo,FUN=mean) I'm looking for a componentwise mean across the two entries of foo, i.e., the following output: [,1] [,2] [,3] [1,] 4 5 6 [2,] 7 8 9 [NB. My "real" application involves trimming and psych::winsor(), so anything that generalizes to this would be extra good.] I've been looking at apply and {s,l,m,t}apply, by, with and aggregate and searched the list archives... any ideas? Thanks a lot, Stephan
Hi Phil, thanks, that already helps: Reduce() gets me the means I need! Unfortunately, Reduce() apparently won't help me with trimming or winsorizing the means, judging from its "successively" philosophy... Any other ideas out there? Best, Stephan Phil Spector schrieb:> Stephan - > If you try to apply mean directly to a matrix, it will just return a > scalar. It's easy to get row means and column > means, but to preserve each element of the matrix, I think > it's better to do the computations directly: > > Reduce('+',foo) / length(foo) > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spector at stat.berkeley.edu > > > On Tue, 30 Dec 2008, Stephan Kolassa wrote: > >> Dear useRs, >> >> I have a list, each entry of which is a matrix of constant dimensions. >> Is there a good way (i.e., not using a for loop) to apply a mean to >> each matrix entry *across list entries*? >> >> Example: >> >> foo <- list(rbind(c(1,2,3),c(4,5,6)),rbind(c(7,8,9),c(10,11,12))) >> some.sort.of.apply(foo,FUN=mean) >> >> I'm looking for a componentwise mean across the two entries of foo, >> i.e., the following output: >> >> [,1] [,2] [,3] >> [1,] 4 5 6 >> [2,] 7 8 9 >> >> [NB. My "real" application involves trimming and psych::winsor(), so >> anything that generalizes to this would be extra good.] >> >> I've been looking at apply and {s,l,m,t}apply, by, with and aggregate >> and searched the list archives... any ideas? >> >> Thanks a lot, >> Stephan >> >> ______________________________________________ >> 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. >> >
on 12/30/2008 08:33 AM Stephan Kolassa wrote:> Dear useRs, > > I have a list, each entry of which is a matrix of constant dimensions. > Is there a good way (i.e., not using a for loop) to apply a mean to each > matrix entry *across list entries*? > > Example: > > foo <- list(rbind(c(1,2,3),c(4,5,6)),rbind(c(7,8,9),c(10,11,12))) > some.sort.of.apply(foo,FUN=mean) > > I'm looking for a componentwise mean across the two entries of foo, > i.e., the following output: > > [,1] [,2] [,3] > [1,] 4 5 6 > [2,] 7 8 9 > > [NB. My "real" application involves trimming and psych::winsor(), so > anything that generalizes to this would be extra good.] > > I've been looking at apply and {s,l,m,t}apply, by, with and aggregate > and searched the list archives... any ideas? > > Thanks a lot, > StephanHow about something like this:> matrix(rowMeans(sapply(foo, c)), dim(foo[[1]]))[,1] [,2] [,3] [1,] 4 5 6 [2,] 7 8 9 Essentially, first create matching elementwise rows:> sapply(foo, c)[,1] [,2] [1,] 1 7 [2,] 4 10 [3,] 2 8 [4,] 5 11 [5,] 3 9 [6,] 6 12 Then, get the row means:> rowMeans(sapply(foo, c))[1] 4 7 5 8 6 9 Then finally restructure the result to a matrix, using the dimensions of foo[[1]]. This of course makes the assumption that each matrix is of the same size and does not otherwise check for that. HTH, Marc Schwartz
I thought this was a good candidate for the plyr package, but it seems that l*ply functions are meant to operate only on separate list elements:> Lists are the simplest type of input to deal with because they are > already naturally > divided into pieces: the elements of the list. For this reason, the > l*ply functions don’t > need an argument that describes how to break up the data structure. > (from: plyr: divide and conquer, Hadley Wickham 2008)Perhaps a new case to consider? Best wishes, baptiste On 30 Dec 2008, at 15:33, Stephan Kolassa wrote:> Dear useRs, > > I have a list, each entry of which is a matrix of constant dimensions. > Is there a good way (i.e., not using a for loop) to apply a mean to > each > matrix entry *across list entries*? > > Example: > > foo <- list(rbind(c(1,2,3),c(4,5,6)),rbind(c(7,8,9),c(10,11,12))) > some.sort.of.apply(foo,FUN=mean) > > I'm looking for a componentwise mean across the two entries of foo, > i.e., the following output: > > [,1] [,2] [,3] > [1,] 4 5 6 > [2_____________________________ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]