I'm using the data.table
package in R. Among other things, data.table causes tables to be passed by reference. Data.table also supports inserting or deleting columns byRef, so you can do things like this...
library('data.table')
AddSquares<-function(DT){
DT[,x2:=(x*x)]
}
DT<-data.table(x=1:3)
AddSquares(DT)
DT
x x2
1: 1 1
2: 2 4
3: 3 9
x2 was created in DT byRef so I didn't have to return the modified table. Easy peezy. Unfortunately, data.table doesn't support inserting or deleting rows byRef, so this following happens...
StripEvens<-function(DT){
DT <- DT[x %% 2 != 0]
}
StripEvens(DT)
DT
x x2
1: 1 1
2: 2 4
3: 3 9
The <-
operator works by value, so it's creating a copy of DT inside the function. If I don't then return DT from then the function then it's 'destroyed' when the function call ends. If I do return DT then I can get back a table that looks the way I want, but I've done so by making a copy, which might as well be passing byVal.
So here's the question. Once DT is passed into my function byRef, how do I make R continue to work on DT byRef? Equivalently, how do I make R treat the DT inside the function as a reference to the same object that DT outside the function references? Is this even possible?