That's not exactly a nested list, but a list of named character vectors. You can apply as.data.frame.list
to each element, then use rbind
. So if x
is your list, then
df <- do.call(rbind, lapply(x, as.data.frame.list, stringsAsFactors = FALSE))
## below is optional - converts character columns to appropriate type
## but will also convert some columns back to factors again
df[] <- lapply(df, type.convert)
df
# code description codeSystem codeSystemVersion
# 1 123.60 not stated as uncontrolled, with neurological manifestations XAZ9CM XAZ9CM-2012
# 2 123.50 not stated as uncontrolled, with ophthalmic manifestations XAZ9CM XAZ9CM-2012
# 3 123.61 not stated as uncontrolled, with neurological manifestations XAZ9CM XAZ9CM-2012
# 4 123.7 peripheral circulatory disorders XAZ9CM XAZ9CM-2012
# 5 123.40 not stated as uncontrolled, with renal manifestations XAZ9CM XAZ9CM-2012
# 6 123.41 not stated as uncontrolled, with renal manifestations XAZ9CM XAZ9CM-2012
# 7 123.5 ophthalmic manifestations XAZ9CM XAZ9CM-2012
# 8 123.53 uncontrolled, with ophthalmic manifestations XAZ9CM XAZ9CM-2012
# 9 123.52 uncontrolled, with ophthalmic manifestations XAZ9CM XAZ9CM-2012
# 10 123.4 renal manifestations XAZ9CM XAZ9CM-2012
Update : You can also do
data.frame(do.call(rbind, x), stringsAsFactors=FALSE)
And other, likely more efficient, possibilities include
library(data.table)
rbindlist(lapply(x, as.list))
and
library(dplyr)
bind_rows(lapply(x, as.data.frame.list, stringsAsFactors=FALSE))
and (thanks to Ananda Mahto)
library(stringi)
data.frame(stri_list2matrix(x, byrow=TRUE), stringsAsFactors=FALSE)
All of these would still require a type conversion on the first column if you wish for it to be numeric.
Also, the data from this question seems to have disappeared, so here it is, copied from the edit history.
x <- list(structure(c("123.60", " not stated as uncontrolled, with neurological manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.50", " not stated as uncontrolled, with ophthalmic manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.61", "not stated as uncontrolled, with neurological manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.7", "peripheral circulatory disorders",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.40", " not stated as uncontrolled, with renal manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.41", " not stated as uncontrolled, with renal manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.5", "ophthalmic manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.53", "uncontrolled, with ophthalmic manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.52", " uncontrolled, with ophthalmic manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")), structure(c("123.4", "renal manifestations",
"XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",
"codeSystemVersion")))