Skip to content

"on=" doesn't retain keys correctly. #1268

Description

@arunsrinivasan

Sample data taken from this SO post from Douglas Clark. Thanks for the nice example!

require(data.table)

# main data table DT, keyed on site and date, with data column x
DT <- data.table(site = rep(LETTERS[1:2], each=3),
                 date = rep(1:3, times=2),
                 x = rep(1:3*10, times=2),
                 key = "site,date")
DT
#    site date  x
#1:    A    1 10
#2:    A    2 20
#3:    A    3 30
#4:    B    1 10
#5:    B    2 20
#6:    B    3 30

# lookup table for x to y lookup, keyed on x
x2y <- data.table(x = c(10,20), y = c(100,200), key = "x")
x2y

To join on column x, we don't have to re-key anymore:

DT[x2y, on="x"]
#    site date  x   y
#1:    A    1 10 100
#2:    B    1 10 100
#3:    A    2 20 200
#4:    B    2 20 200

key(DT[x2y, on="x"])
# [1] "site" # <~~~ wrong

It should be x as i here is keyed on that column, and corresponding column in DT is also named x.

Activity

  1. jan-glx commented on Sep 10, 2015

    @jan-glx
    Contributor

    I would prefer if the result was indeed keyed on "site,date".

  2. arunsrinivasan commented on Sep 10, 2015

    @arunsrinivasan
    MemberAuthor

    The order of rows in joined result is determined by i. So no, that'd not be possible.

  3. added this to the v1.9.8 milestone on Sep 22, 2015
  4. arunsrinivasan commented on Mar 9, 2016

    @arunsrinivasan
    MemberAuthor

    Updated SO post linked above.

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