onezero: Converts a vector to all 1/0s

View source: R/onezero.R

onezeroR Documentation

Converts a vector to all 1/0s

Description

Converts a vector into ones and zeroes, any values which fall outside of what is set by the arguments gets converted to missing. Option to retain missing values if nothing is passed to zero.

Usage

onezero(x, one_values, zero_values, one_threshold, keep.na = TRUE)

Arguments

x

A vector.

one_values

Vector of values in x to receive values of 1. Cannot be used in combination with one_threshold.

zero_values

Vector of values in x to receive values of 0. Values not specified in this argument will be turned into missing. If this argument is left empty, all remaining values assumed to receive value of 0. Cannot be used in combination with one_threshold.

one_threshold

Character string describing threshold for determining 1/0. Must follow a logical-operator-then-value format, using <, <=, >, >=, ==, or !=. Cannot be used in combination with one_values or zero_values.

keep.na

Only needed if one_values is specified and zero_values is not, whether or not to retain missing values during conversion, default TRUE.

Value

A numeric vector of ones and zeros.

See Also

is_onezero()

Examples


x <- c("one", "zero", "foo", NA)
y <- 1:4

onezero(x, one_values = "one")
onezero(x, one_values = "one", keep.na = FALSE)
onezero(x, one_values = "one", zero_values = "zero")
onezero(x, one_values = "one", zero_values = c("zero", "foo"))
onezero(x, one_values = "bar")
onezero(x, one_values = "bar", keep.na = FALSE)
onezero(x, one_values = "bar", zero_values = "baz")
onezero(y, one_threshold = "<= 2")


ttrodrigz/onezero documentation built on May 9, 2023, 2:59 p.m.