Learn R Programming

batman (version 0.1.0)

to_logical: Convert categorical representations of true/false values to a logical

Description

to_logical is designed for the situation where you have categorical data (perhaps from a survey, or direct user input) that represents TRUE/FALSE values as something other than TRUE/FALSE - "Yes", "No", "None", "Y" or "False", say. With to_logical you can easily convert a vector of these values into an actual, logical vector, using either a predefined set of accepted TRUE or FALSE equivalents, or a set you specify yourself.

Usage

to_logical(x, language = "en", custom_true = character(), custom_false = character())

Arguments

x
a vector of categorical TRUE/FALSE/NA values.
language
the language to use. See get_languages for the list of supported languages. If your language is not supported, you can use custom_true and custom_false to provide values.
custom_true
a vector of values to consider, in addition to the ones to_logical already recognises, TRUE. Empty by default. Note that the comparison code is case-insensitive, so there's no need to include (for example) both "ja" and "Ja".
custom_false
a vector of values to consider, in addition to the ones to_logical already recognises, FALSE. Empty by default; see the notes on case sensitivity above.

Examples

Run this code
# A very simple example using the pre-known true and false equivalents
categorical_values <- c("true","t","y","yes","f","no","1")
to_logical(categorical_values)

# Use a custom specifier
categorical_values <- c("NA","NA","NA","NA","NA","NA","NA","NA","Batman")
to_logical(categorical_values, custom_true = c("Batman"))

Run the code above in your browser using DataLab