I would like to write a wrapper around the user-defined function that takes some vectors as input (eg mtcars$hp, mtcars$ametc.) to enter the input as the name of the data frame (as a parameter data, for example mtcars) and variable names (eg: hpand am), as usual in most standard functions.
But I have some problems, my suggested function is "demo" (wrapping around meandoes not work.
the code:
f <- function(x, data=NULL) {
if (!missing(data)) {
with(data, mean(x))
} else {
mean(x)
}
}
Running against vector works, of course:
> f(mtcars$hp)
[1] 146.69
But withunsuccessfully unsuccessful:
> f(hp, mtcars)
Error in with(d, mean(x)) : object 'hp' not found
In a global environment / without my user function it works correctly:
> with(mtcars, mean(hp))
[1] 146.69
substitute, deparse , - . !