Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use character string as function argument

Tags:

function

string

r

I'm sure this is simple, but I cannot find a solution ... I would like to use a variable containing a character string as argument for a function.

x <- c(1:10) myoptions <- "trim=0, na.rm=FALSE" 

Now, something like

foo <- mean(x, myoptions) 

should be the same as

foo <- mean(x, trim=0, na.rm=FALSE) 

Thanks in advance!

like image 900
Kilian Avatar asked Oct 20 '11 13:10

Kilian


1 Answers

You can use eval and parse:

foo <- eval(parse(text = paste("mean(x,", myoptions, ")"))) 
like image 121
kohske Avatar answered Sep 23 '22 02:09

kohske