1

I am experiencing odd behavior with data.table when I change a copied version of dataset. Here is code that reproduces the error for me:

library(data.table)
set.seed(02138)
data1 <- data.table(var1 = 1:10, var2 = sample(letters, 10))
data2 <- data1
data2[var1 == 3, var3 := 1]
data2[var1 != 3, var3 := 0]

I get the following when I print out the original and copied data.tables:

> data1
    var1 var2 var3
 1:    1    n    0
 2:    2    p    0
 3:    3    i    1
 4:    4    k    0
 5:    5    e    0
 6:    6    d    0
 7:    7    a    0
 8:    8    x    0
 9:    9    y    0
10:   10    b    0
> data2
    var1 var2 var3
 1:    1    n    0
 2:    2    p    0
 3:    3    i    1
 4:    4    k    0
 5:    5    e    0
 6:    6    d    0
 7:    7    a    0
 8:    8    x    0
 9:    9    y    0
10:   10    b    0

Notice that the new variable has been created both in the copied version of the data.table and in the old version. I only want it in the new version. What is going on here?

data.table version: 1.9.4

R version: 3.1.1

OS: Mac OS X Yosemite 10.10

lish
  • 192
  • 9

0 Answers0