A more nuanced response is returned than the standard R method, which does not return NAs if x is all NAs. Has useful applications in understanding a set of categorical variables belonging to a single question. E.g. A question on brand usage across 10 product types to understand 'any' usage of a brand x.

any_x(x, value)

Arguments

x

Vector of values to test.

value

Value to test whether it exists in x. NA is returned if none exists at all.

Value

A logical vector whether a value exists in x, and returns NA if x contains only NAs.

Examples

any_x(c(1,0,1),1) # TRUE
#> [1] TRUE
any_x(c(1,NA,1),1) # TRUE
#> [1] TRUE
any_x(c(0,0,NA),1) # FALSE
#> [1] FALSE
any_x(c(NA,NA,NA),1) # NA
#> [1] NA