我试图解决R函数中输入变量操作的问题。 功能如下:
testFun<-function(x,y){ x+y }我想以这样的方式运行:
testFun(x=1,y=5+x)但是生成了错误:
浏览[1]> testFun(x = 1,y = 5 + x) 换行时出错:找不到对象'x'
从论证评估的角度来看,这是一个错误。 所以我需要将'x'设置为默认值或外部值(我在http://adv-r.had.co.nz/Functions.html#function-arguments中扫描过的详细信息)。
所以我的问题是,是否有可能在一行中进行这样的操作,而不是制作包装或外部值?
它应该有输出:
跑:
testFun(X = 1,Y = 5 + x)的
输出:
7
I am trying to solve the issue of input variable manipulation in R function. The function is as follows:
testFun<-function(x,y){ x+y }I want to run in such a way:
testFun(x=1,y=5+x)but the error is generated:
Browse[1]> testFun(x=1,y=5+x) Error during wrapup: object 'x' not found
From the argument evaluation poin of view it is a mistake. So I need to have 'x' either a default value or outside value (details I have scanned in http://adv-r.had.co.nz/Functions.html#function-arguments).
So my question is whether it is possible to make such a manipulation in one row, without making a wrappers or outside values?
It is supposed to have the output:
run:
testFun(x=1,y=5+x)
output:
7
最满意答案
您可以使用<-但它将在您的全局环境中创建变量。 请参阅以下示例:
testFun(x <- 1, y = 5 + x) # [1] 7You can use <- but it will create the variable in your global environment. See the following for example:
testFun(x <- 1, y = 5 + x) # [1] 7更多推荐
发布评论