请考虑以下列表:
> l1 <- list(NULL,1,2,list(NULL,3,list(NULL,4)))
> str(l1)
List of 4
$ : NULL
$ : num 1
$ : num 2
$ :List of 3
..$ : NULL
..$ : num 3
..$ :List of 2
.. ..$ : NULL
.. ..$ : num 4
Run Code Online (Sandbox Code Playgroud)
要从NULL第一级删除值,只需调用即可
l1[vapply(l1,is.null,logical(1L))] <- NULL
Run Code Online (Sandbox Code Playgroud)
现在我想删除所有NULL级别的所有值,我想出了以下代码.
list.clean <- function(.data, fun = is.null, recursive = FALSE) {
if(recursive) {
.data <- lapply(.data, function(.item) {
if(is.list(.item)) list.clean(.item, fun, TRUE)
else .item
})
}
.data[vapply(.data,fun,logical(1L))] <- NULL
.data
}
Run Code Online (Sandbox Code Playgroud)
并打电话
> list.clean(l1, recursive = TRUE)
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[[3]][[1]]
[1] 3
[[3]][[2]]
[[3]][[2]][[1]]
[1] 4
Run Code Online (Sandbox Code Playgroud)
虽然它现在可以使用,但有更好或更快的方法吗?
G. *_*eck 18
这可以递归完成:
rmNull <- function(x) {
x <- Filter(Negate(is.null), x)
lapply(x, function(x) if (is.list(x)) rmNull(x) else x)
}
l2 <- rmNull(l1)
Run Code Online (Sandbox Code Playgroud)
赠送:
> str(l2)
List of 3
$ : num 1
$ : num 2
$ :List of 2
..$ : num 3
..$ :List of 1
.. ..$ : num 4
Run Code Online (Sandbox Code Playgroud)