Convert Mixed-Length named List to data.frame
dataframe, r
Solution
Here's my initial thought. It doesn't speed up your approach, but it does simplify the code considerably:
# makeDF <- function(List, Names) {
# m <- t(sapply(List, function(X) unlist(X)[Names],
# as.data.frame(m)
# }
## vapply() is a bit faster than sapply()
makeDF <- function(List, Names) {
m <- t(vapply(List,
FUN = function(X) unlist(X)[Names],
FUN.VALUE = numeric(length(Names))))
as.data.frame(m)
}
## Test timing with a 50k-item list
ll <- createList(50000)
nms <- c("a", "b", "c")
system.time(makeDF(ll, nms))
# user system elapsed
# 0.47 0.00 0.47
Problem
I have a list of the following format: ``` [[1]] [[1]]$a [1] 1 [[1]]$b [1] 3 [[1]]$c [1] 5 [[2]] [[2]]$c [1] 2 [[2]]$a [1] 3 ``` There is a predefined list of possible "keys" (`a`, `b`, and `c`, in this case) and each element in the list ("row") will have values defined for one or more of these keys. I'm looking for a fast way to get from the list structure above to a data.frame which would look like the following, in this case: ``` a b c 1 1 3 5 2 3 NA 2 ``` Any help would be appreciated! Appendix I'm dealing with a table that will have up to 50,000 rows and 3-6 columns, with most of the values specified. I'll be taking the table in from JSON and trying to quickly get it into data.frame structure. Here's some code to create a sample list of the scale with which I'll be working: ``` ids <- c("a", "b", "c") createList <- function(approxSize=100){ set.seed(1234) fifth <- round(approxSize/5) list <- list() list[1:(fifth*5)] <- rep( list(list(a=1, b=2, c=3), list(a=3, b=4, c=5), list(a=7, c=9), list(c=6, a=8, b=3), list(b=6)), fifth) list } ``` Just create a list with `approxSize` of 50,000 to test the performance on a list of this size.