【发布时间】:2020-07-01 19:26:56
【问题描述】:
我有以下功能:
DT <- data.table(col1 = 1:4, col2 = c(2:5))
fun <- function(DT, fct){
DT_out <- DT[,new_col := fct]
return(DT_out)
}
fun(input, fct = function(x = col1, y = col2){y - x})
实际上我在这段代码sn-p之前和之后都有一些处理,因此我不希望直接使用带有固定fct的语句DT[,new_col := fct](因为fct应该是灵活的)。我知道这个问题与this 非常相似,但我无法弄清楚如何重新编写代码,以便允许将两列作为函数的参数。上面的代码给出了错误:
Error in `[.data.table`(DT, , `:=`(new_col, fct)) :
RHS of assignment is not NULL, not an an atomic vector (see ?is.atomic) and not a list column.
【问题讨论】:
-
你想要
new_col := fct(col1,col2)吗?实际上,您正在为new_col分配一个函数,而不是它的输出 -
不,这不能满足我的需求,因为我希望参数像下面给出的答案一样灵活!不过还是谢谢
标签: r function input data.table multiple-columns