Skip to content

Changed behaviour in R 3.1.2 and data.table 1.9.4 when cross-joining two keyed data.tables #959

Closed

Description

@stephlocke

I use cross-joining a reasonable amount and wrote a function CJ.dt() #814 . Since the latest upgrades, this now NAs the values in the first table referenced. MRE below:

library(data.table)
a  <- data.table(a=1:2, b=letters[1:2])
b  <- data.table(c=3:4, d=letters[3:4])
CJ.dt<-function(X,Y) {
  stopifnot(is.data.table(X),is.data.table(Y))
    k <- NULL # Setting the variables to NULL first for CRAN check NOTE
  setkey(X[,c(k=1,.SD)],k)[Y[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL]
}

#This works with unkeyed tables
ab<-CJ.dt(a,b) 
ab

#This doesn't work with keyed tables
setkey(a,a)
setkey(b,c)

ab<-CJ.dt(a,b) 
ab

SessionInfo

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C            LC_COLLATE=C         LC_MONETARY=C        LC_MESSAGES=C        LC_PAPER=C           LC_NAME=C            LC_ADDRESS=C        
[10] LC_TELEPHONE=C       LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.9.4

loaded via a namespace (and not attached):
[1] Rcpp_0.11.3   chron_2.3-45  plyr_1.8.1    reshape2_1.4  stringr_0.6.2 tools_3.1.2

Activity

  1. arunsrinivasan commented on Nov 17, 2014

    @arunsrinivasan
    Member

    When opening an issue, there should've been a link asking to go through the contribution guidelines, which also links to the new How to file a bug report page. Please read that.

    The reason the results are different is because data.table v1.9.4+ is smarter (in retaining keys).

    require(data.table) # v1.9.2
    b = data.table(c=3:4, d=letters[3:4])
    setkey(b, c)
    key(b[, c(k=1, .SD)])
    # NULL
    
    require(data.table) # v1.9.5
    b = data.table(c=3:4, d=letters[3:4])
    setkey(b, c)
    key(b[, c(k=1, .SD)])
    # [1] "c"

    It knows that there's no need to leave out the key here, as the data is still sorted by the key you set. So the join is happening against k of column a and c of b. You could just add setkey(Y, NULL) to your function to fix it:

    library(data.table)
    a  <- data.table(a=1:2, b=letters[1:2])
    b  <- data.table(c=3:4, d=letters[3:4])
    CJ.dt = function(X,Y) {
      stopifnot(is.data.table(X),is.data.table(Y))
      k = NULL
      X = X[, c(k=1, .SD)]
      setkey(X, k)
      Y = Y[, c(k=1, .SD)]
      setkey(Y, NULL)
      X[Y, allow.cartesian=TRUE][, k := NULL][]
    }
    
    # works
    ab = CJ.dt(a,b) 
    ab
    #    a b c d
    # 1: 1 a 3 c
    # 2: 2 b 3 c
    # 3: 1 a 4 d
    # 4: 2 b 4 d
    
    # and works as well
    setkey(a,a)
    setkey(b,c)
    
    ab = CJ.dt(a,b) 
    #    a b c d
    # 1: 1 a 3 c
    # 2: 2 b 3 c
    # 3: 1 a 4 d
    # 4: 2 b 4 d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions