在R函数的参数集中操作输入变量(to manipulate input variable in the set of arguments of R function)

编程入门 行业动态 更新时间:2024-10-23 09:40:33
在R函数的参数集中操作输入变量(to manipulate input variable in the set of arguments of R function)

我试图解决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] 7

You 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

更多推荐

本文发布于:2023-07-31 01:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   函数   参数   操作   manipulate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!