Perform a semi-join with data.table

后端 未结 8 1352
天命终不由人
天命终不由人 2020-11-27 16:44

How do I perform a semi-join with data.table? A semi-join is like an inner join except that it only returns the columns of X (not also those of Y), and does not repeat the r

8条回答
  •  迷失自我
    2020-11-27 17:37

    I'm confused with all the not-joins above, isn't what you want simply:

    unique(x[y, .SD])
    #   x y
    #1: 1 a
    

    If x can have duplicate keys, then you can unique y instead:

    ## Creating an example data.table 'a' three-times-repeated first row 
    x <- data.table(x = c(1,1,1,2), y = c("a", "a", "a", "b"))
    setkey(x, x)
    y <- data.table(x = c(1, 1), z = 10:11)
    setkey(y, x)
    
    x[eval(unique(y, by = key(y))), .SD] # data.table >= 1.9.8 requires by=key(y)
    #    x y
    # 1: 1 a
    # 2: 1 a
    # 3: 1 a
    

提交回复
热议问题