根据另一个 data.table 的行提取 data.table 的行

Extract rows of data.table according to rows of another data.table

我找不到任何答案,但我认为这很容易做到。

我有这个data.table:

DT = expand.grid(Season = c("Winter","Spring","Summer","Fall"),
                 Station = c("A","B","C"),
                 Group = c("1","2","3","4"))
DT$Value = seq(1,length(DT[,1]),1)
DT = data.table(DT)

我想根据这个other data.table:

得到DT的一个子集
indexTable = data.table(Season = c("Winter","Spring","Spring"),
                        Station = c("B","B","A"),
                        Group = c("1","2","3"))

基本上我只想要 DT 中包含在 indexTable 中的行。预期的结果是 table:

expectedTable = data.table(Season = c("Winter","Spring","Spring"),
                           Station = c("B","B","A"),
                           Group = c("1","2","3"),
                           Value = c(5,18,26))

我正在尝试使用此代码获取:

tryTable = DT[DT$Station %in% indexTable$Station &
              DT$Season %in% indexTable$Season &
              DT$Group %in% indexTable$Group,]

这不仅给了我想要的 3 行,还给了我 DT.

的其他行

我做错了什么?有没有一种简单的方法可以使用 data.table 索引符号(例如使用 setkey?)

来获取 expectedTable

您正在对两个表进行 INNER JOIN。

DT[
    indexTable
    , on = c("Season", "Station", "Group")
    , nomatch = 0
]

   Season Station Group Value
1: Winter       B     1     5
2: Spring       B     2    18
3: Spring       A     3    26

参考