Dominik.Cullmann at Forst.bwl.de
2008-Dec-12 09:55 UTC
[R] recursive List extraction question
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>
Try this: rapply(L, c)[grep("\\.val$", names(rapply(L, c)))] On Fri, Dec 12, 2008 at 7:55 AM, <Dominik.Cullmann@forst.bwl.de> wrote:> 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@forst.bwl.de > <mailto:dominik.cullmann@forst.bwl.de> > Homepage: www.fva-bw.de <http://www.fva-bw.de> > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]