使用字符串作为函数参数 [英] Use character string as function argument
本文介绍了使用字符串作为函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我确信这很简单,但我找不到解决方案...
我想使用一个包含字符串的变量作为函数的参数。
x < - c(1:10)
myoptions< - trim = 0,na.rm = FALSE
现在,类似于
foo< - mean(x,myoptions)
应该与
相同foo < - mean(x,trim = 0,na.rm = FALSE)
pre>
预先感谢!
解决方案您可以使用
eval
和parse
:
foo < - eval(parse(text = paste(mean(x,,myoptions,))))
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!
解决方案You can use
eval
andparse
:foo <- eval(parse(text = paste("mean(x,", myoptions, ")")))
这篇关于使用字符串作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文