Reputation: 6755
List-to-dataframe have been extensively discussed here but I couldn't find a solution for quite a simple problem: a list of lists with varying number of items.
This is the list
require(json)
hdi <- fromJSON(file="http://data.undp.org/resource/y8j2-3vi9.json")
with the first list of the list being
hdi[1]
[[1]]
[[1]]$`_2011_hdi_value`
[1] "0.887"
[[1]]$`_1990_hdi_value`
[1] "0.798"
[[1]]$`_2000_2013_average_annual_hdi_growth`
[1] "0.37"
[[1]]$`_1980_hdi_value`
[1] "0.757"
[[1]]$`_2010_hdi_value`
[1] "0.885"
[[1]]$`_2008_hdi_value`
[1] "0.879"
[[1]]$`_1990_2000_average_annual_hdi_growth`
[1] "0.62"
[[1]]$`_2012_hdi_value`
[1] "0.889"
[[1]]$`_2013_hdi_value`
[1] "0.890"
[[1]]$`_2005_hdi_value`
[1] "0.870"
[[1]]$`_2000_hdi_value`
[1] "0.849"
[[1]]$country
[1] "Very high human development"
[[1]]$`_1980_1990_average_annual_hdi_growth`
[1] "0.52"
yet the number of items across lists varies
summary(as.numeric(summary(hdi)[,"Length"]))
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 5.00 16.00 16.00 14.93 16.00 16.00
I want to build a dataframe with 16 columns with NA
indicating the missing value for that list.
Upvotes: 1
Views: 597
Reputation: 7373
Easiest is
data <- jsonlite::fromJSON("http://data.undp.org/resource/y8j2-3vi9.json")
Or, using rlist
library(rlist)
data <- list.stack(hdi, fill=TRUE)
Upvotes: 4
Reputation: 24480
You can try this:
require(RJSONIO)
hdi <- fromJSON("http://data.undp.org/resource/y8j2-3vi9.json")
#get the unique values of the column names
columnnames<-unique(unlist(lapply(hdi,names)))
#subset each element of the list with and rbind them together
res<-do.call(rbind,lapply(hdi,function(x) x[columnnames]))
colnames(res)<-columnnames
From there, you can coerce the numeric column and coerce the object to a data.frame
. I suggest you to use the RJSONIO
library to handle conversion from/to json as it seems to be more reliable in my experience.
Upvotes: 1