[R] recursive List extraction question

Dominik.Cullmann at Forst.bwl.de Dominik.Cullmann at Forst.bwl.de
Fri Dec 12 10:55:48 CET 2008


Dear all,
I've got a list 
L <- list(L1 = list
          (foo = "bar"
           , SL = NULL
           )
          , L2 = list
          (
           foo = "bar"
           , SL = list
           (SSL1 = list
            (DF = data.frame(val = 21, foo = "bar")
             , DFOO = list(foo = "foo", bar = "bar")
             )
            , SSL2 = list
            (DF = data.frame(val = 22, foo = "bar")
             , DFOO = list(foo = "foo", bar = "bar")
             )
            )

           )
          , L3 = list
          (
           foo = "bar"
           , SL = list
           (SSL1 = list
            (DF = data.frame(val=31, foo="bar")
             , DFOO = list(foo="foo", bar="bar")
             )
            )
           )
          )
from which I'ld like to extract the values 21,22 and 31.

lapply(
       lapply(
              lapply(
                     lapply(L, "[[","SL")
                     ,"[[",1)
              ,"[[","DF")
       ,"[[","val") 

gives me 21 and 31. Because of lapply(...,"[[",1), it searches  through
the first element of each SL, which are the SSL1. It misses SSL2. Is
there a way to replace the ,"[[",1), with a regex or the like to get the
job done? Does anybody now any other solution? 
Thanks and regards,
Dominik


----------------------------------------------
Andreas Dominik Cullmann
Forstliche Versuchs- und Forschungsanstalt 
Wonnhalde 4 
79100 Freiburg 
Tel. +49 761 4018 204 
Email: dominik.cullmann at forst.bwl.de
<mailto:dominik.cullmann at forst.bwl.de> 
Homepage: www.fva-bw.de <http://www.fva-bw.de> 



More information about the R-help mailing list