Skip to content

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

Closed

Description

@riabusan

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]

Activity

  1. jangorecki commented on Mar 23, 2015

    @jangorecki
    Member

    Good MRE! same on 1.9.5.

  2. added this to the v1.9.6 milestone on Mar 24, 2015
  3. cguill95 commented on Mar 25, 2015

    @cguill95

    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
    
  4. arunsrinivasan commented on May 10, 2015

    @arunsrinivasan
    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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions