[R] convert a list to a data frame

Sam Steingold sds at gnu.org
Wed Apr 4 17:11:05 CEST 2012


I have a huge list (returned by fromJSON) with elements like this:

$`zz/3260`
$`zz/3260`$name
[1] "myname"

$`zz/3260`$status
[1] "active"

$`zz/3260`$vectors
$`zz/3260`$vectors$`vector/241`
$`zz/3260`$vectors$`vector/241`$channel
[1] "channel/300"

$`zz/3260`$vectors$`vector/241`$targets
$`zz/3260`$vectors$`vector/241`$targets[[1]]
$`zz/3260`$vectors$`vector/241`$targets[[1]]$range_start
[1] "0"

$`zz/3260`$vectors$`vector/241`$targets[[1]]$range_end
[1] "99"

I want a data frame with these column values for the above list element:
zz=3260, name="myname", status="active", vector=241, channel=300
range_start=0, range_end=99

(and sometimes $vectors is an empty list, yuk!)

my plan is to
1 extract a list of fields
2 convert it to a vector (which I know how to put into a data frame)

how do I do that?
I can get "zz/3260" with names() and split it with gsub().
I can get "myname" with as.vector(unlist(sapply(a,"[",1))))

is there a shortcut?

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000
http://www.childpsy.net/ http://truepeace.org http://mideasttruth.com
http://memri.org http://honestreporting.com http://jihadwatch.org
If you try to fail, and succeed, which have you done?



More information about the R-help mailing list