[R] looping through lists...

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Feb 8 22:11:27 CET 2002


Greg Trafton <trafton at itd.nrl.navy.mil> writes:

> nw.1 <- c(nw.201.1,nw.204.1,nw.206.1)
> 
> but that did not preserve the individual list structure I needed.
> 
> and of course I use more than just RL, so I want to be able to
> arbitrarily get information from each list.
> 
> ideally, I'd like to be able to do something like this:
> 
> getmean(nw.1, "RL")
> [1] 15.630  2.713 18.811
> 
> any suggestions on how to cycle through lists or assign lists to a
> variable that keeps their list-structure?

Something like 

nw.1 <- list(nw.201.1, nw.204.1, nw.206.1)
sapply(nw.1, function(x) mean(x$RL))

should do it, or generally

getmean <- function(l,n) sapply(l, function(x) mean(x[[n]]))

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list