user3294195 user3294195 - 7 months ago 37
R Question

Return a function object with arguments partially filled

I want to define an R function that uses an existing function, with some arguments filled in. For instance, suppose I want to define a function

as follows:

meanNA <- mean(na.rm = TRUE)

The idea is that when I call
meanNA(x, trim = 0)
I will call
mean(x, trim = 0, na.rm = TRUE)
. Is this even possible in R? If so, what's the right way to implement this?


Just take your example, we can write:

meanNA <- function (...) mean(..., na.rm = TRUE)


x <- c(1:4, NA)
# [1] 2.5