binarize.vals: Convert other formats to binary flags.

Description Usage Arguments Value Description Warning Examples

View source: R/misc funs.R

Description

Convert other formats to binary flags.

Usage

1
binarize.vals(x, val0, val1, ignore.case = TRUE)

Arguments

x

a vector.

val0, val1

the values to convert to 0/1 respectively.

ignore.case

if TRUE, conversion will ignore

as.numeric

if TRUE, return a numeric vector else return a string.

Value

A new vector of zeroes and ones.

Description

Flags/indicators are stored in different formats. This function converts two arbitrary values to 0/1. Others will be converted to NA. The default behavior ignores case, e.g. binarize.vals(x, 'Y', 'N') will convert 'Y' to 0 but 'y' to NA. Setting ignore.case = FALSE will first convert all

Warning

TRUE is not 'true'! Setting ignore.case = FALSE when converting T/F will not work as desired. The function should catch this by checking is.logical(x).

Examples

1
2
3
4
5
6
x <- c(sample(c('Y', 'N'), 5, replace = T), 'a', 'y', 0, 'n')
print(x)
binarize.vals(x, 'Y', 'N')

x <- c(T, F, T, T, F)
binarize.vals(x, 'T', 'F', ignore.case = F)  # wrong

zane-uw/edwHelpers documentation built on Nov. 24, 2019, 2:53 p.m.