如何将可选参数传递给 do.call 中的函数

how to pass optional arguments to function inside do.call

我希望以编程方式创建一个变量名数组,例如:

desired_output <- c("first_purchase_date","last_purchase_date","largest_purchase_date",
                    "first_purchase_amount","last_purchase_amount","largest_purchase_amount") 

我相信我可以用 do.call 做到这一点,建立在类似的东西上:

> do.call(paste, expand.grid(c("first","last","largest"),c("date","amount")))
[1] "first date"     "last date"      "largest date"   "first amount"   "last amount"    "largest amount"

但是我不太明白如何在 do.call 中将 sep="_purchase_" 参数传递给 paste。在 ?do.call 我读到

args is a list of arguments to the function call. The names attribute of args gives the argument names.

试图合并这个,我试过:

df <- expand.grid(c("first","last","largest"), 
                  c("date","amount"), 
                  stringsAsFactors = FALSE)

do.call(paste, args = list(...=df, sep="_purchase_"))
# does not return desired output, but instead:
# [1] "c(\"first\", \"last\", \"largest\", \"first\", \"last\", \"largest\")"
# [2] "c(\"date\", \"date\", \"date\", \"amount\", \"amount\", \"amount\")"  

通过 do.call 生成 desired_output 的正确方法是什么?

你可以 do.call(paste, c(df, sep="_purchase_")),但也许 apply(df, 1, paste, collapse="_purchase_") 更直接。