Data.table: [R-Forge #2298] If the RHS of := is recycled plonk, there's no column copy, tripping up setkey() and more later.

Created on 8 Jun 2014  路  2Comments  路  Source: Rdatatable/data.table

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

keyvar count count1 count2

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

keyvar count count1 count2

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

keyvar count count1 count2

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.

bug

Most helpful comment

Somehow, the issue has reappeared (#2540). The old bugfix by Matt (a0b27e824e470ae5359bf77b0294deb9dffd66ed) must have fallen victim to code refactoring.
Existing tests do not spot the issue reliably.

All 2 comments

a0b27e824e470ae5359bf77b0294deb9dffd66ed

Somehow, the issue has reappeared (#2540). The old bugfix by Matt (a0b27e824e470ae5359bf77b0294deb9dffd66ed) must have fallen victim to code refactoring.
Existing tests do not spot the issue reliably.

Was this page helpful?
0 / 5 - 0 ratings