使用单个对象将多个参数传递给函数? [英] Using a single object to pass multiple arguments to a function?

查看:95
本文介绍了使用单个对象将多个参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个不能被更改的功能,例如:

Let's say I have a function that can't be altered, like:

add.these <- function(x,y,z) {
  x + y + z
}

我想将所有三个参数作为单个对象传递.如何将单个对象传递给函数,以便将它们作为单独的输入求值?

And I want to pass all three arguments as a single object. How do I pass this single object through to the function so it evaluates them as separate inputs?

理想的结果是类似args <- list(x,y,z)的结果,而add.these(args)返回结果.

The ideal result would be something like args <- list(x,y,z), and add.these(args) returns the result.

这是一个困扰我的简单问题,但我一直无法解决.实际的用例是该函数具有可变数量的参数,取决于所需的输出,我想将这些参数作为列表或其他内容传递给我们.

It's a simple question that's been bothering me but I've stupidly been unable to figure it out. The actual use case is that the function has variable numbers of arguments it requires depending on the desired outputs, and I want to pass these through as a list or something.

推荐答案

您在寻找do.call吗?

> args=list(1,2,3)
> do.call(add.these,args)
[1] 6

这篇关于使用单个对象将多个参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆