[英]How to write a function with an unspecified number of arguments where the arguments are column names
我正在嘗試使用 ... 編寫一個具有未指定數量的參數的函數,但我遇到了這些參數是列名的問題。 舉個簡單的例子,如果我想要一個函數,它接受一個數據框並使用 within() 來創建一個新列,該列將其他幾個列粘貼在一起,我會直觀地將其寫為
example.fun <- function(input,...){
res <- within(input,pasted <- paste(...))
res}
其中 input 是一個數據框,而 ... 指定列名。 這給出了一個錯誤,指出無法找到列名(它們被視為對象)。 例如
df <- data.frame(x = c(1,2),y=c("a","b"))
example.fun(df,x,y)
這將返回“粘貼錯誤(...):找不到對象'x'”
我可以在函數中使用 attach() 和 detach() 作為解決方法,
example.fun2 <- function(input,...){
attach(input)
res <- within(input,pasted <- paste(...))
detach(input)
res}
這可行,但如果在全局環境中碰巧有一個對象與列名被稱為相同的東西,它會很笨重並且會遇到問題,所以這不是我的偏好。
這樣做的正確方法是什么?
謝謝
1)將代碼包裝在 eval(substitute(...code...)) 中,如下所示:
example.fun <- function(data, ...) {
eval(substitute(within(data, pasted <- paste(...))))
}
# test
df <- data.frame(x = c(1, 2), y = c("a", "b"))
example.fun(df, x, y)
## x y pasted
## 1 1 a 1 a
## 2 2 b 2 b
1a)一種變體是:
example.fun.2 <- function(data, ...) {
data.frame(data, pasted = eval(substitute(paste(...)), data))
}
example.fun.2(df, x, y)
2)另一種可能性是將每個參數轉換為字符串,然后使用索引。
example.fun.3 <- function(data, ...) {
vnames <- sapply(substitute(list(...))[-1], deparse)
data.frame(data, pasted = do.call("paste", data[vnames]))
}
example.fun.3(df, x, y)
3)其他可能性是更改函數的設計並將變量名稱作為公式或字符向量傳遞。
example.fun.4 <- function(data, formula) {
data.frame(data, pasted = do.call("paste", get_all_vars(formula, data)))
}
example.fun.4(df, ~ x + y)
example.fun.5 <- function(data, vnames) {
data.frame(data, pasted = do.call("paste", data[vnames]))
}
example.fun.5(df, c("x", "y"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.