safe_ifelse: Vectorized 'ifelse' with Class Coercion

Description Usage Arguments Value Examples

Description

The normal behavior for the ifelse function is to return the resulting vector with a class type coerced to be the same as the yes argument. However, when a Date type vector is passed as an argument, Dates lose their class when the condition is evaluated. The resulting vector is of type numeric. This function safe_ifelse modifies the normal behavior for Date vectors. Users can specify whether to coerce to any class or the default will be used which is class(no).

Usage

1
safe_ifelse(cond, yes, no, class_to_use = class(no))

Arguments

cond

the test to determine if yes/no

yes

the value or operation to return if cond evaluates to TRUE

no

the value or operation to return if cond evaluates to FALSE

Value

the resulting vector coerced to class type class_to_use

Examples

1
2
d = as.Date(c("2015-01-01","2015-08-08"))
safe_ifelse(d=="2015-01-01",d-10,d)

Tsunamical28/mdpr documentation built on May 9, 2019, 5:15 p.m.