I have functions that has a lot of arguments. So I would like to create a list of arguments and pass them to the function.
As an example, take ?mean
function:
mean(x, trim = 0, na.rm = FALSE, ...)
So lets say I want to calculate mean of 1:10
, assigned to variable x
, but pass other arguments as a list:
gm <- list (trim = 0, na.rm = FALSE)
mean(1:10, gm)
#R> Error in mean.default(1:10, gm) : 'trim' must be numeric of length one
I tried to use do.call
but do not work either.
do.call(mean,list(1:10, gm))
#R> Error in mean.default(1:10, list(trim = 0, na.rm = FALSE)) :
#R> 'trim' must be numeric of length one
As noted in a comment, your gm
has the wrong shape for do.call
, it interprets your option list as a trim
argument.
To make the argument correct shape, use c
:
gm <- list(trim=0, na.rm=FALSE)
do.call(mean, c(list(1:10), gm))
[1] 5.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With