将嵌套列表中的元素转换为数据框
Converting elements in a nested list to dataframe
我有如下嵌套列表,
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
需要帮助将这些元素转换为数据框。
这不完全是一个 嵌套 列表,而是一个命名字符向量列表。您可以对每个元素应用 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")))
我有如下嵌套列表,
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
需要帮助将这些元素转换为数据框。
这不完全是一个 嵌套 列表,而是一个命名字符向量列表。您可以对每个元素应用 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")))