我有一个嵌套列表,其中包含NULL元素,我想用其他东西替换它们。例如:
l <- list(
NULL,
1,
list(
2,
NULL,
list(
3,
NULL
)
)
)
我希望将NULL元素替换为NA。最自然的方法是使用rapply
递归地循环遍历列表。我尝试过:
rapply(l, function(x) NA, classes = "NULL", how = "replace")
rapply(l, function(x) if(is.null(x)) NA else x, how = "replace")
很遗憾,这两种方法都不起作用,因为 rapply
显然会忽略 NULL
元素。
我该如何操作嵌套列表中的 NULL
元素?
rapply2(l, function(x) if (is.null(x)) NA else x)
就能正常工作了。 - rawrrapply2()
的行为。 - jazzurro