1

I am trying to remove column but it removes from all other tables e.g. you see when I removed column from dd2 then it also removes column from dd1.

library("data.table")
dd1 <- mtcars
setDT(dd1)
dd2 <- dd1
dd2 <- dd2[,cyl:=NULL]
str(dd1)
str(dd2)

I can use dd2$cyl <- NULL but just trying to see there better option using data.table.

R007
  • 101
  • 1
  • 13
  • 3
    That's expected behaviour. When you do `dd2 <- dd1` with a *data.table* you are just creating another reference to the same object. This is different behaviour from a standard *data.frame* - see https://stackoverflow.com/questions/10225098/understanding-exactly-when-a-data-table-is-a-reference-to-vs-a-copy-of-another – thelatemail Jun 04 '20 at 22:42
  • @R007 Do you have any other question unrelated to this – akrun Jun 04 '20 at 22:55
  • Thanks @akrun, I got it that it happens due to reference usage in data.table. – R007 Jun 04 '20 at 23:09
  • To clarify - when you do `dd2 <- dd1` you are just creating a reference to a new object, whether *data.frame* or *data.table*. The difference is using `:=` assignment in *data.table* won't force a copy - one of the answers to the previously linked question describes this scenario - https://stackoverflow.com/a/14293056/496803 – thelatemail Jun 05 '20 at 00:01

0 Answers0