list.clean {rlist} | R Documentation |
Clean a list by a function
Description
This function removes all elements evaluated to be
TRUE
by an indicator function. The removal can be recursive
so that the resulted list surely does not include such elements in
any level.
Usage
list.clean(.data, fun = is.null, recursive = FALSE)
Arguments
.data |
A |
fun |
A |
recursive |
|
Details
Raw data is usually not completely ready for analysis, and needs to
be cleaned up to certain standards. For example, some data operations
require that the input does not include NULL
values in any
level, therefore fun = "is.null"
and recursive = TRUE
can be useful to clean out all NULL
values in a list at any
level.
Sometimes, not only NULL
values are undesired,
empty vectors or lists are also unwanted. In this case,
fun = function(x) length(x) == 0L
can be useful to remove
all empty elements of zero length. This works because
length(NULL) == 0L
, length(list()) == 0L
and
length(numeric()) == 0L
are all TRUE
.
Examples
x <- list(a=NULL,b=list(x=NULL,y=character()),d=1,e=2)
list.clean(x)
list.clean(x, recursive = TRUE)
list.clean(x, function(x) length(x) == 0L, TRUE)