fifelse.Rdfifelse is a faster and more robust replacement of ifelse. It is comparable to dplyr::if_else and hutils::if_else. It returns a value with the same length as test filled with corresponding values from yes, no or eventually na, depending on test. Supports bit64's integer64 and nanotime classes.
fifelse(test, yes, no, na=NA)
| test | A logical vector. |
|---|---|
| yes, no | Values to return depending on |
| na | Value to return if an element of |
In contrast to ifelse attributes are copied from yes to the output. This is useful when returning Date, factor or other classes.
A vector of the same length as test and attributes as yes. Data values are taken from the values of yes and no, eventually na.
#> [1] 0 1 3 4 3 1 0 1 3 4# unlike ifelse, fifelse preserves attributes, taken from the 'yes' argument dates = as.Date(c("2011-01-01","2011-01-02","2011-01-03","2011-01-04","2011-01-05")) ifelse(dates == "2011-01-01", dates - 1, dates)#> [1] 14974 14976 14977 14978 14979fifelse(dates == "2011-01-01", dates - 1, dates)#> [1] "2010-12-31" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05"#> [1] 1 1 3#> [1] a a c #> Levels: a b c#> [1] 1 1 1 1 1 0 0 0 0 0 0 2