funN | R Documentation |
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.
funN(n, fun, ...)
n |
the number of minimum observations for evaluation |
fun |
the function to be evaluated |
... |
additional arguments passed to the function (e.g. na.rm = ) |
a function that will calculate fun
on a given vector if at least
n
valid observations are present
Tim Appelhans
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.