make_chr_logi: Coerce one or more values in a character vector to logical...

View source: R/make_chr_logi.R

make_chr_logiR Documentation

Coerce one or more values in a character vector to logical TRUE

Description

Transform a character into logical with denoted values as TRUE, with NA handling options

Usage

make_chr_logi(x, val_as_TRUE = NULL, preserve_NA = TRUE, NA_as_what = NA)

Arguments

x

Input character vector to coerce to logical

val_as_TRUE

A character vector containing the values to coerce to TRUE. Including NA will override both preserve_NA and NA_as_what

preserve_NA

Should NA be preserved? Acts as a safety flag, and NA_as_what is ignored unless this is set to FALSE; defaults to TRUE

NA_as_what

How should NA be handled? One of TRUE, FALSE, NA; defaults to NA. Ignored if preserve_NA is TRUE

Details

This function is most useful for its explicit handling of NA. By default, NA is passed-through. If NA is included within val_as_TRUE, then all NA will be set to TRUE. Alternatively, if preserve_NA is set to FALSE and NA_as_what is set to either TRUE or FALSE, NA will be set as requested.

Non-character inputs to either x or val_as_TRUE will raise an error. Furthermore, if x is all NA, an error is raised.

Value

A logical vector of length x

Examples

x <- c("yes", "YES", NA, "no", "NO", "NA", "0")
make_chr_logi(x, c("yes", "YES"))
make_chr_logi(x, c("yes", "YES", NA)) # overrides preserve_NA or NA_as_what

slin30/wzMisc documentation built on Jan. 27, 2023, 1 a.m.