[R] List of lists to data frame?
rkevinburton at charter.net
Thu Nov 17 03:39:10 CET 2011
Say I have the following data:
> s <- list()
> s[["A"]] <- list(name="first", series=ts(rnorm(50), frequency=10,
> s[["B"]] <- list(name="second", series=ts(rnorm(60), frequency=10,
If I use unlist since this is a list of lists I don't end up with a data
frame. And the number of rows in the data frame should equal the number of
time series entries. In the sample above it would be 110. I would expect
that the name and category strings would be recycled for each row. My brute
force code attempts to build the data frame by appending to the master data
frame but like I said it is *very* slow.
From: R. Michael Weylandt [mailto:michael.weylandt at gmail.com]
Sent: Wednesday, November 16, 2011 5:26 PM
To: rkevinburton at charter.net
Cc: r-help at r-project.org
Subject: Re: [R] List of lists to data frame?
unlist(..., recursive = F)
On Wed, Nov 16, 2011 at 6:20 PM, <rkevinburton at charter.net> wrote:
> I would like to make the following faster:
> df <- NULL
> for(i in 1:length(s))
> df <- rbind(df, cbind(names(s[i]), time(s[[i]]$series),
> as.vector(s[[i]]$series), s[[i]]$category))
> names(df) <- c("name", "time", "value", "category")
> The s object is a list of lists. It is constructed like:
> s[["object"]] <- list(. . . . . .)
> where "object" would be the name associated with this list
> s[[i]]$series is a 'ts' object and s[[i]]$category is a name.
> Constructing this list is reasonably fast but to do some more
> processing on the data it would be easier if it were converted to a data
> Right now the above code is unacceptably slow at converting this list
> of lists to a data frame. Any suggestions on how to optimize this are
> Thank you.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help