safe_ifelse: Preserves class when using ifelse

View source: R/safe_ifelse.R

safe_ifelseR Documentation

Preserves class when using ifelse

Description

This is just a cool, fun little function that I found on Stack Overflow, https://stackoverflow.com/questions/6668963/how-to-prevent-ifelse-from-turning-date-objects-into-numeric-objects. Note that the function dplyr::if_else() is probably a better way to do the same thing since it is unit-tested and dcoumented in a CRAN package. Just preserving this here.

Usage

safe_ifelse(cond, true, false)

Arguments

cond

A logical condition

true, false

Values to use for TRUE and FALSE values of cond. Attributes are taken from true.

Value

Where cond is TRUE, the matching value from true, where it's FALSE, the matching value from false

Examples

dates <- as.Date(c("2011-01-01",
                   "2011-01-02",
                   "2011-01-03",
                   "2011-01-04",
                   "2011-01-05"))

# This shows the issue with regular ifelse
dates <- ifelse(dates == "2011-01-01", dates - 1, dates)
dates
class(dates)

# Try out safe_ifelse
dates <- safe_ifelse(dates == "2011-01-01", dates - 1, dates)
dates
class(dates)

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.