我有如下嵌套列表,
dput( list(structure(c("123.60", " on"))))
我有兴趣将此嵌套列表中的元素转换为数据框。 例如,输出应如下所示。
code description
123.60 not stated as uncontrolled, with neurological manifestations
123.50 not stated as uncontrolled, with ophthalmic manifestations
.
.
.
123.52 uncontrolled, with ophthalmic manifestations
需要帮助将这些元素转换为数据框。
答案 0 :(得分:6)
这不是一个嵌套列表,而是一个命名字符向量列表。您可以将as.data.frame.list
应用于每个元素,然后使用rbind
。因此,如果x
是您的列表,那么
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
更新:您也可以
data.frame(do.call(rbind, x), stringsAsFactors=FALSE)
其他可能更有效的可能性包括
library(data.table)
rbindlist(lapply(x, as.list))
和
library(dplyr)
bind_rows(lapply(x, as.data.frame.list, stringsAsFactors=FALSE))
和(感谢Ananda Mahto)
library(stringi)
data.frame(stri_list2matrix(x, byrow=TRUE), stringsAsFactors=FALSE)
如果你希望它是数字的话,所有这些仍然需要在第一列上进行类型转换。
此外,这个问题的数据似乎已经消失了,所以在这里,它是从编辑历史中复制的。
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")))