Skip to content

List of lists to data frame?

6 messages · rkevinburton at charter.net, R. Michael Weylandt, Kevin Burton +3 more

#
unlist(..., recursive = F)

Michael
On Wed, Nov 16, 2011 at 6:20 PM, <rkevinburton at charter.net> wrote:
#
Say I have the following data:
start=c(2000,1)), category="top")
start=c(2000,2)), category="next")

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.

Kevin

-----Original Message-----
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)

Michael
On Wed, Nov 16, 2011 at 6:20 PM, <rkevinburton at charter.net> wrote:
frame.
#
On Wed, Nov 16, 2011 at 9:39 PM, Kevin Burton <rkevinburton at charter.net> wrote:
Appending is very slow, and should be avoided. Instead, create a data frame
of the correct size before starting the loop, and add each new bit into the
appropriate place.

There may well be a more efficient solution (I don't quite understand
what your objective is), but simply getting rid of the rbind() within a
loop will help.

  
    
#
I don't know if this is faster, but ...

out <- do.call(rbind,
   lapply(s, function(x)data.frame(x$category,x$name,as.vector(x$series))))

## You can then name the columns of out via names()

Note: No fancy additional packages are required.

-- Bert
On Wed, Nov 16, 2011 at 6:39 PM, Kevin Burton <rkevinburton at charter.net> wrote: