I faced a problem when exercising with data.table. Here is my problem. I wrote a simple subtraction function:
minus <- function(a, b){
return(a - b)
}
My dataset is a simple data.table:
dt <- as.data.table(data.frame(first=c(5, 6, 7), second=c(1,2,3)))
dt
first second
1 5 1
2 6 2
3 7 3
I would like to write another function,
myFunc <- function(dt, FUN, ...){
return(dt[, new := FUN(...)])
}
The usage is simply:
res <- myFunc(dt, minus, first, second)
and the result would be the following:
res
first second new
1: 5 1 4
2: 6 2 4
3: 7 3 4
How can I archive such a goal? Thanks!