Perform a semi-join with data.table

后端 未结 8 1370
天命终不由人
天命终不由人 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

    The package dplyr supports the following four join types:

    inner_join, left_join, semi_join, anti_join

    So for the semi-join try the following code

    library("dplyr")
    
    table1 <- data.table(x = 1:2, y = c("a", "b"))
    table2 <- data.table(x = c(1, 1), z = 10:11)
    
    semi_join(table1, table2)
    

    The output is as expected:

    # Joining by: "x"
    # Source: local data table [1 x 2]
    # 
    #       x     y
    #   (int) (chr)
    # 1     1     a
    

提交回复
热议问题