funN: create a function conditional to a set number of observations

View source: R/funN.R

funNR Documentation

create a function conditional to a set number of observations

Description

This function is a so-called closure. It will set up a function so that it will only be evaluated if the vector on which it is called has at least n observations. If it doesn't, it will return NA.

Usage

funN(n, fun, ...)

Arguments

n

the number of minimum observations for evaluation

fun

the function to be evaluated

...

additional arguments passed to the function (e.g. na.rm = )

Value

a function that will calculate fun on a given vector if at least n valid observations are present

Author(s)

Tim Appelhans

Examples

## create some dummy data
vec <- rnorm(100, 5, 1)
vec[sample(length(vec), 20)] <- NA #replace 20 values of vec with NA

f <- funN(70, mean, na.rm = TRUE)
f(vec)

f1 <- funN(90, mean, na.rm = TRUE)
f1(vec)

f3 <- funN(80, quantile, na.rm = TRUE)
f3(vec)

f4 <- funN(81, quantile, na.rm = TRUE)
f4(vec)


environmentalinformatics-marburg/Rsenal documentation built on July 28, 2023, 6:09 a.m.