On 12/16/2014 06:06 PM, SH wrote:
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?

If efficiency is of concern, do not use data.frame() but create a list and add the required attributes with data.table::setattr (the setattr function of the data.table package). (You can also consider creating a data.table instead of a data.frame.)

# some largish lists
a1 <- list(x = rnorm(1e6), y = rnorm(1e6))
a2 <- list(x = rnorm(1e6), y = rnorm(1e6))
a3 <- list(x = rnorm(1e6), y = rnorm(1e6))

# amount of memory allocated
gc(reset=TRUE)

# get names of the objects
out_names <- ls(pattern="a[[:digit:]]$")

# create a list
out <- lapply(lapply(out_names, get), "[[", "x")

# note that no copying occured
gc()

# decorate the list
data.table::setattr(out, "names", out_names)
data.table::setattr(out, "row.names", seq_along(out[[1]]))
class(out) <- "data.frame"

# still no copy
gc()

# output
head(out)


HTH,
  Denes



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@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.


______________________________________________
R-help@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.

Reply via email to