[R-lang] apply through a list

Stefan Th. Gries stgries at gmail.com
Wed May 16 14:26:31 PDT 2007


How about this?

# your data
(M1 <- matrix( 1:4, nrow=2, ncol=2))
(M2 <- matrix( 5:8, nrow=2, ncol=2))
(L <- list(M1, M2))

# setting up a matrix for the results
M.res<-M1

# filling the matrix
for (i in 1:length(M.res)) {
   M.res[i]<-mean(sapply(as.vector(L), "[", i)) # apply some function, in
this example the mean

}
M.res # look at the results

STG
-- 
Stefan Th. Gries
-----------------------------------------------
University of California, Santa Barbara
http://www.linguistics.ucsb.edu/faculty/stgries
-----------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://ling.ucsd.edu/pipermail/r-lang/attachments/20070516/844c659d/attachment.htm 


More information about the R-lang mailing list