A function to sum 2 vectors
vsum = \(s) s[1] + s[2]
works well for data.frame
> vsum(expand.grid(1:2,2:3))
Var1
1 3
2 4
3 4
4 5
but how to get the same result for data.table
?
This is incorrect result:
> library(data.table)
> vsum(CJ(1:2,2:3))
V1 V2
<int> <int>
1: 2 5
Additional notes
I do not want the function with two variables.
Changing the function to
vsum = \(s){ s[,1,drop=F] + s[,2,drop=F] }
should give the desired result for both, data.frame
and data.table
.
Choosing columns with s[1]
is a data.frame
shortcut. With data.table
it picks the row.