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.
0
or1
). 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() :
0
and1
are kept, all other values are transformed to NA.logical() :
TRUE
andFALSE
are tansformed to1
and0
.
- logical
Boolean defining if the output should be a logical vector instead of a numeric vector (i.e.
0
and1
becomesFALSE
and `TRUE).
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