小编Fed*_*ico的帖子

使用“ ...” R分割函数的数组参数

以下函数创建一个具有n列作为参数数量的data.frame

functionWithDots <- function(...) {
  f1 <- function(x) c(x,x+4)
  list <- list(...)
  list <- lapply(list, f1)
  expand.grid(list)
  }
Run Code Online (Sandbox Code Playgroud)

作为functionWithDots(1,2)运行时,预期结果是:

  • id Var1 Var2
    1. 1 2
    2. 5 2
    3. 1 6
    4. 5 6

而如果我通过用“ 1:2”替换“(1,2)”来做同样的事情,

functionWithDots(1,2)
Run Code Online (Sandbox Code Playgroud)

结果是

  • id Var1
    1. 1个
    2. 2
    3. 5
    4. 6

我如何将正确的未连接参数传递给该函数,因为传递时似乎返回不同的结果,比如说,“ 1,2,3”而不是“ c(1,2,3)”?

arguments r function

5
推荐指数
1
解决办法
84
查看次数

标签 统计

arguments ×1

function ×1

r ×1