Transform a vector to a binary vector.
All values that are not 0, 1, TRUE,
FALSE, or NA are transformed to NA.
Arguments
- vect
A character, factor, logical or numeric vector corresponding to a binary variable (i.e.
0or1). The following values are recognized:character() or factor() : "TRUE", "FALSE", "0", "1", "NA" will be respectively transformed to
1,0,0,1,NA. Spaces and case are ignored. All other values will be transformed to NA.numeric() :
0and1are kept, all other values are transformed to NA.logical() :
TRUEandFALSEare tansformed to1and0.
- logical
Boolean defining if the output should be a logical vector instead of a numeric vector (i.e.
0and1becomesFALSEand `TRUE).- default
The default value to use for the values that are not recognized. By default,
NAis used, but it can be0or1.
Examples
vect_to_binary(
c(0, 1, 2, 3.6, "TRUE", "FALSE", "0", "1", "NA", "B", TRUE, FALSE, NA)
)
#> Warning: NAs introduced by coercion
#> [1] 0 1 NA NA 1 0 0 1 NA NA 1 0 NA