A vectorised if or else function. It checks that the true or false (or the optional missing) arguments have the same type. However it accepts a generic NA. Built upon dplyr's [if_else()] function. The only difference is that the user do not have to specify the type of NA. if_else_ is faster than base [ifelse()] and a tad slower than dplyr's [if_else()]. Attributes are taken from either true or false because one generic NA.
if_else_(condition, true, false, missing = NULL)
a vector
logical vector
value to replace if condition is true. Must be same length as condition or 1.
value to replace if condition is false. Must be same length as condition or 1.
optional. a replacement if condition returns NA. Must be same length as condition or 1.
If the returning vector have attributes (e.g. for factors) it returns the attributes for the first non-generic NA in the order true, false and then missing.
vignette("s")
, vignette("hablar")
v <- c(TRUE, FALSE, TRUE, FALSE)
if_else_(v, "true", "false")
v <- c(TRUE, FALSE, NA, FALSE)
if_else_(v, 1, NA, 999)
Run the code above in your browser using DataLab