if_else2: if_else2

Description Usage Arguments Details Value Examples

View source: R/utility-functions.R

Description

Custom ifelse function that evaluates missing (NA) values. If the logical argument (x) compares to a value that is 'NA', it is set to 'FALSE'

Usage

1
if_else2(x, a, b)

Arguments

x

A logical argument

a

value if 'x' is 'TRUE'

b

value if 'x' is 'FALSE'

Details

unlike the base ifelse() function, if_else2() is able to evaluate NA as either a or b. In base ifelse(), anything compared to NA will produce NA, which can break a function. When dealing with large datasets like the CCHS, there are many missing (NA) values. That means a special ifelse function like if_else2() is needed in order for other functions to not break

Value

a or b based on the evaluation of x

Examples

1
2
3
4
5
6
7
age <- 12
status <- if_else2((age < 18), "child", "invalid age")
print(status)

age <- NA
status <- if_else2((age < 18), "child", "invalid age")
print(status)

cchsflow documentation built on June 24, 2021, 1:07 a.m.