When I used to remove columns, I would always do something like:
DT[, Tax:=NULL]
Sometimes to make a backup, I would do something like
DT2 <- DT
But just a second ago this happened:
library(data.table)
DT <- structure(list(Province = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,
3), Tax = c(2000, 3000, 1500, 3200, 2000, 1500, 4000, 2000, 2000,
1000, 2000, 1500), year = c(2000, 2000, 2000, 2001, 2001, 2001,
2002, 2002, 2002, 2003, 2003, 2003)), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
DT2 <- structure(list(Province = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,
3), Tax = c(2000, 3000, 1500, 3200, 2000, 1500, 4000, 2000, 2000,
1000, 2000, 1500), year = c(2000, 2000, 2000, 2001, 2001, 2001,
2002, 2002, 2002, 2003, 2003, 2003)), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
setDT(DT)
setDT(DT2)
DT2 <- DT
# Removes Tax in BOTH datasets !!
DT2[, Tax:=NULL]
I remember something about this when starting to learn about data.table, but obviously this is not really desirable (for me at least).
What is the proper way to deal with this without accidentally deleting columns?