Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up
Submitted by: Timothee Carayol; Assigned to: Nobody; R-Forge link
I say it's scary because it returns no error and is potentially quite difficult to detect in existing code..
FYI I tried this on data.table 1.8.2 and R 2.14.1.
A <- data.table(
keyvar=rev(letters[1:10]),
count=1:10
)
A[, c(
'count1',
'count2'
) := count, with=F
]
A
1: j 1 1 1
2: i 2 2 2
3: h 3 3 3
4: g 4 4 4
5: f 5 5 5
6: e 6 6 6
7: d 7 7 7
8: c 8 8 8
9: b 9 9 9
10: a 10 10 10
All looks good.
Let's make a copy of A; call it B. We'll use it later on.
B <- copy(A)
Now let's set a key on "keyvar" in A:
setkeyv(A, 'keyvar')
A
1: a 10 1 1
2: b 9 2 2
3: c 8 3 3
4: d 7 4 4
5: e 6 5 5
6: f 5 6 6
7: g 4 7 7
8: h 3 8 8
9: i 2 9 9
10: j 1 10 10
That's the scary bit. "keyvar" and "count" got sorted nicely, but "count1" and "count2" are now matched to wrong rows.
Let's try the same thing on the copy of A that we made earlier..
setkeyv(B, 'keyvar')
B
1: j 1 1 1
2: i 2 2 2
3: h 3 3 3
4: g 4 4 4
5: f 5 5 5
6: e 6 6 6
7: d 7 7 7
8: c 8 8 8
9: b 9 9 9
10: a 10 10 10
That's fine!
I.e. whatever was wrong with A, is fixed in its copy B.