A faster base R alternative to ifelse()
.
ifelse()
is a base R function that takes a logical test
vector, a value if test
is TRUE
and a value if test
is FALSE
. It can be thought as "vectorized" if-else, which takes a vector as an input, tests each of its elements using some criteria and returns another vector whose elements are based on the test result for each element of the input vector.
Here is a simple example where ifelse()
returns "one"
if a vector element equals 1, and "not one"
otherwise: