【发布时间】:2016-06-17 22:29:32
【问题描述】:
所以,我有一个这样的列表,并且想要转换为一个向量,其中空列表被替换为 NA。列表中的所有条目总是最多只有一个元素(感谢 MongoDB,它只返回嵌套元素作为列表)。
有没有比循环(应用系列)更有效的方法?
dput(l)
list(structure(list(), .Names = character(0)), structure(list(
postcode = "27612"), .Names = "postcode"), structure(list(
postcode = "30127"), .Names = "postcode"), structure(list(
postcode = "35173"), .Names = "postcode"), structure(list(
postcode = "30047"), .Names = "postcode"), structure(list(
postcode = "87571"), .Names = "postcode"))
sapply(l, function(x) if (length(x)) unlist(x$postcode) else NA)
[1] NA "27612" "30127" "35173" "30047" "87571"
输出正是我想要的,但担心在非常大的数据集上,这会很慢。希望有更快的方法。
【问题讨论】:
标签: r