繁体   English   中英

R:将省略号与需要任意数量参数的函数一起使用

[英]R: Using ellipsis with a function that takes a arbitrary number of arguments

很多次,我发现自己输入以下内容

print(paste0(val1,',',val2,',',val3)) to print the output from a function with variables separated by a comma.

当我要从输出中复制生成一个csv文件时,它很方便。

我想知道是否可以在R中编写一个为我完成此功能的函数。 经过多次尝试,我只能做到以下几点。

ppc <- function(string1,string2,...) print(paste0(string1,',',string2,',',...,))

最多三个参数时,它效果很好。

> ppc(1,2,3)
[1] "1,2,3"
> ppc(1,2,3,4)
[1] "1,2,34" 

ppc(1,2,3,4)应该具有"1,2,3,4" 如何纠正我的功能? 我以某种方式相信这在R中是可能的。

您无需编写自己的函数。 您可以使用paste

paste(1:3,collapse=",")
# [1] "1,2,3"

或者,如果您坚持使用ppc()函数:

ppc <- function(...) paste(...,sep=",")
ppc(1,2,3,4)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM