在R中使用可变数量的参数创建函数, [英] Creating a function in R with variable number of arguments,

查看:102
本文介绍了在R中使用可变数量的参数创建函数,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中创建函数时,我们通常会指定参数的数量,例如

When creating a function in R, we usually specify the number of argument like

function(x,y){
}

这意味着它仅需要两个参数.但是,如果未指定参数数量(一种情况下,我必须使用两个参数,而另一种情况下,我必须使用三个或更多参数),我们该如何处理这个问题?我对编程非常陌生,因此将不胜感激示例.

That means it takes only two arguments. But when the numbers of arguments are not specified (For one case I have to use two arguments but another case I have to use three or more arguments) how can we handle this issue? I am pretty new to programming so example will be greatly appreciated.

推荐答案

d <- function(...){
    x <- list(...) # THIS WILL BE A LIST STORING EVERYTHING:
    sum(...)       # Example of inbuilt function
}

d(1,2,3,4,5)

[1] 15 

这篇关于在R中使用可变数量的参数创建函数,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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