Lazyifelse: Lazy version of ifelse

Description Usage Arguments Details Examples

View source: R/Misc.R

Description

When using ifelse, the return values are evaluated for all values, then discarded. This causes problems when you want to check for valid inputs in test. In this function, values are only evaluated when necessary: yFun is called with yIn for test==TRUE, and nFun is called with nIn for test==FALSE. Missing (NA) values in test return NA, without calling either yFun or nFun

Usage

1
Lazyifelse(test, yFun, yIn, nFun, nIn)

Arguments

test

an object which can be coerced to logical

yFun, nFun

Functions to be called when test evaluates to TRUE or FALSE respectively

yIn, nIn

Arguments for yFun and nFun. Recycled if necessary

Details

If you just want to return values, you can use the function identity

Examples

1
2
3
stats <- list(a=NA,
              b=list(time="1:00", speed=100))
besttimes <- Lazyifelse(is.na(stats), identity, NA, function(x) {sapply(x, `[[`, i='time')}, stats)

EmilBode/EmilMisc documentation built on Feb. 24, 2020, 4:11 p.m.