[R] looping through lists...

Greg Trafton trafton at itd.nrl.navy.mil
Fri Feb 8 21:02:34 CET 2002


Hi, all.  I have a whole group of lists with things like this:

nw.1$cond (a string)
nw.1$RL (a vector of 10 values)
nw.1$IL (a vector of 10 values)

and I have lots of these lists:
nw.201
nw.202
nw.203
...

what I'd like to do is be able to get specific values from all these
lists (like the mean of $RL) for each indivividual list.  I can
certainly do this manually
mean(c(nw.201.1$RL,nw.204.1$RL,nw.206.1$RL)) to get the overall mean
or write some simple code to return a vector of means:
[1] 15.630  2.713 18.811
(the problem with my current function is that I have to hard code the
individual list names which is what I'm trying to avoid).

but what I'd rather do is have a list of lists that I can then cycle
through to get the individual values for.  mostly, I just don't want
to have to type in the (nw.201.1$RL,nw.204.1$RL,nw.206.1$RL) every
time I want the values.  I tried
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?

thanks!
greg
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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