Skip to contents

Returns TRUE if all values in a logical vector are TRUE. A stricter, surprise-free version of base::all().

Usage

all2(x, na.rm = FALSE)

Arguments

x

(logical)
The logical vector to be checked if all elements are TRUE. Any non-logical input returns FALSE.

na.rm

(Scalar logical: c(FALSE, TRUE))
Whether or not to remove NAs from x before evaluating.

Value

Scalar logical

Details

Suppose you have a numeric vector y and want to validate all values are greater than zero using base::all(y > 0). If y contains NAs and all nonmissing elements are greater than 0, the result will be NA. You then might use base::all(y > 0, na.rm = TRUE), however, this assumes NAs are ok to exclude. In this scenario, all2(y > 0) would return FALSE instead of NA.

all(NULL) and all(logical(0)) both return TRUE. all2() returns FALSE for both cases.

all(NA, na.rm = TRUE) returns TRUE. all2(NA, na.rm = TRUE) returns FALSE.

See also

Examples

#----------------------------------------------------------------------------
# all2() examples
#----------------------------------------------------------------------------
library(bkbase)

x <- c(1, 2, 3, NA)

all2(x > 0)
#> [1] FALSE
all(x > 0)
#> [1] NA

all2(x > 0, na.rm = TRUE)
#> [1] TRUE
all(x > 0, na.rm = TRUE)
#> [1] TRUE

all2(NULL)
#> [1] FALSE
all(NULL)
#> [1] TRUE

all2(logical(0))
#> [1] FALSE
all(logical(0))
#> [1] TRUE

all2(numeric(0))
#> [1] FALSE
all(numeric(0))
#> [1] TRUE

all2(NA, na.rm = TRUE)
#> [1] FALSE
all(NA, na.rm = TRUE)
#> [1] TRUE

all2(NA, na.rm = FALSE)
#> [1] FALSE
all(NA, na.rm = FALSE)
#> [1] NA