Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior on dt[J(d2), a := b, nomatch = 0] #1092

Closed
riabusan opened this issue Mar 23, 2015 · 3 comments
Closed

Strange behavior on dt[J(d2), a := b, nomatch = 0] #1092

riabusan opened this issue Mar 23, 2015 · 3 comments
Assignees
Labels
Milestone

Comments

@riabusan
Copy link

Hi,

I am using a magnificent data.table package version 1.9.4. Thanks a lot for such useful software masterpiece.

In example below, data.table complains that columns o and n are of different length while I don't see a reason why they should be different.

library(data.table)

tr = data.table(k = c(0, 2, 3, 7), o = "b", key = "k")
en = data.table(k = c(1, 2, 3, 4, 5), n = c("n1", "n2", "n3", "n4", "n5"), key = "k")

tr_copy = copy(tr)

## Gives a warning about recycling and produces wrong result, 
## why `o` and `n` of a different length, aren't they supposed to be equal after join?
tr_copy[en, o := n, nomatch = 0]

## Warning message: In `[.data.table`(tr_copy, en, `:=`(o, n), nomatch = 0) :  Supplied 2 items to be assigned to 5 items of column 'o' (recycled leaving remainder of 1 items).

#######################################################
#######################################################

tr_copy = copy(tr)

## No error and gives correct result, seems counterintuitive
tr[en, o := n, nomatch = NA]
@jangorecki
Copy link
Member

Good MRE! same on 1.9.5.

@arunsrinivasan arunsrinivasan self-assigned this Mar 24, 2015
@arunsrinivasan arunsrinivasan added this to the v1.9.6 milestone Mar 24, 2015
@cguill95
Copy link

Hi,

I am also extensively using this wonderful package and I encoutered the same kind of strange behaviour, but in addition to what riabusan said I would add two things :
1) First, sometimes you don't even get a warning which makes it really tricky. For instance if I slightly modify riabusan's example by adding 0, 6, 7 and the corresponding n0, n6, n7 into his "en" variable it gives :

library(data.table)
tr = data.table(k = c(0, 2, 3, 7), o = "b", key = "k")
en = data.table(k = c(0, 1, 2, 3, 4, 5, 6, 7), n = c("n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7"), key = "k")
tr[en, "o":=n, nomatch=0L]

> tr
   k  o
1: 0 n0
2: 2 n3
3: 3 n7
4: 7 n7
  1. Second, for some weird reason when you add have a duplicate key in the "tr" variable it works just fine (I duplicated the 2 here)
library(data.table)
tr = data.table(k = c(0, 2, 2, 3, 7), o = "b", key = "k")
en = data.table(k = c(0, 1, 2, 3, 4, 5, 6, 7), n = c("n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7"), key = "k")
tr[en, "o":=n, nomatch=0L]

> tr
   k  o
1: 0 n0
2: 2 n2
3: 2 n2
4: 3 n3
5: 7 n7

@arunsrinivasan
Copy link
Member

Thanks for the reports. Now fixed, although (the current behaviour of) nomatch=0L has no effect with :=.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants