Learn R Programming

rlist (version 0.4.2.3)

list.clean: Clean a list by a function

Description

This function removes all elements evaluated to be TRUE by a 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 list or vector
fun
A character or a function that returns TRUE or FALSE to indicate if an element of .data (its sublists) should be removed.
recursive
logical. Should the list be cleaned recursively?

Details

Raw data is usually not completely ready for analysis before they are cleaned by 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

Run this code
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)

Run the code above in your browser using DataLab