[R] Extract values from multiple lists

David L Carlson dcarlson at tamu.edu
Tue Dec 16 19:33:29 CET 2014


Something like

scens <- paste0("scen", 1:N)
new.df <- data.frame(sapply(scens, function(x) get(x)[["pop.inf.r"]]))

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of SH
Sent: Tuesday, December 16, 2014 11:06 AM
To: r-help
Subject: [R] Extract values from multiple lists

Dear List,

I hope this posting is not redundant.  I have several list outputs with the
same components.  I ran a function with three different scenarios below
(e.g., scen1, scen2, and scen3,...,scenN).  I would like to extract the
same components and group them as a data frame.  For example,
pop.inf.r1 <- scen1[['pop.inf.r']]
pop.inf.r2 <- scen2[['pop.inf.r']]
pop.inf.r3 <- scen3[['pop.inf.r']]
...
pop.inf.rN<-scenN[['pop.inf.r']]
new.df <- data.frame(pop.inf.r1, pop.inf.r2, pop.inf.r3,...,pop.inf.rN)

My final output would be 'new.df'.  Could you help me how I can do that
efficiently?

Thanks in advance,

Steve

P.S.:  Below are some examples of summary outputs.


> summary(scen1)
                Length Class  Mode
aql                1   -none- numeric
rql                1   -none- numeric
alpha              1   -none- numeric
beta               1   -none- numeric
n.sim              1   -none- numeric
N                  1   -none- numeric
n.sample           1   -none- numeric
n.acc              1   -none- numeric
lot.inf.r          1   -none- numeric
pop.inf.n       2000   -none- list
pop.inf.r       2000   -none- list
pop.decision.t1 2000   -none- list
pop.decision.t2 2000   -none- list
sp.inf.n        2000   -none- list
sp.inf.r        2000   -none- list
sp.decision     2000   -none- list
> summary(scen2)
                Length Class  Mode
aql                1   -none- numeric
rql                1   -none- numeric
alpha              1   -none- numeric
beta               1   -none- numeric
n.sim              1   -none- numeric
N                  1   -none- numeric
n.sample           1   -none- numeric
n.acc              1   -none- numeric
lot.inf.r          1   -none- numeric
pop.inf.n       2000   -none- list
pop.inf.r       2000   -none- list
pop.decision.t1 2000   -none- list
pop.decision.t2 2000   -none- list
sp.inf.n        2000   -none- list
sp.inf.r        2000   -none- list
sp.decision     2000   -none- list
> summary(scen3)
                Length Class  Mode
aql                1   -none- numeric
rql                1   -none- numeric
alpha              1   -none- numeric
beta               1   -none- numeric
n.sim              1   -none- numeric
N                  1   -none- numeric
n.sample           1   -none- numeric
n.acc              1   -none- numeric
lot.inf.r          1   -none- numeric
pop.inf.n       2000   -none- list
pop.inf.r       2000   -none- list
pop.decision.t1 2000   -none- list
pop.decision.t2 2000   -none- list
sp.inf.n        2000   -none- list
sp.inf.r        2000   -none- list
sp.decision     2000   -none- list

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list