Are all values TRUE
all2.RdReturns TRUE if all values in a logical vector are TRUE.
A stricter, surprise-free version of base::all().
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.
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