- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1k
 
Open
Labels
Description
This is something we should basically be aware of. R v3.1.0+ has nice changes and cleverly avoids deep copies wherever possible. And therefore, scenarios like this, however rare, might happen. And we should provide a fix here.
DF1 <- data.frame(x=1:5, y=6:10, z=11:15)
DF2 <- transform(DF1, z = 16:20)
require(data.table)
setorder(setDT(DF2), -z)
DF1
#   x  y  z
#1 5 10 11
#2 4  9 12
#3 3  8 13
#4 2  7 14
#5 1  6 15
DF2
#    x  y  z
#1: 5 10 20
#2: 4  9 19
#3: 3  8 18
#4: 2  7 17
#5: 1  6 16The fact that x and y are shallow copied results in DF1's columns being affected as well.
On a side note, this also means that if one were to write a function to operate on DF2 using the C API (by reference), then the same thing would happen.