safe: Safely apply a function

Description Usage Arguments Value Author(s) Examples

View source: R/transform.R

Description

Convenience function to safely apply a function when the argument may have length zero.

Usage

1
safe(FUN, x, ...)

Arguments

FUN

a function to apply

x

first argument for FUN

...

additional arguments passed to FUN

Value

The result of FUN(x, ...) after preprocessing x, or NA if x has length zero after preprocessing.

Author(s)

Toby Johnson Toby.x.Johnson@gsk.com

Examples

1
2
3
4
5
6
7
8
## Not run: 
x <- 1:5
y <- rep("A", 5)
x[y == "B"]
min(x[y == "B"]) ## produces warning and returns Inf
safe(min, x[y == "B"]) ## returns NA

## End(Not run)

tobyjohnson/gtx documentation built on Aug. 30, 2019, 8:07 p.m.