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

时间:2018-04-06 01:23:52

标签: r indexing data.table subset extraction

我找不到任何答案,但我觉得这很容易。

我有这个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)

我想根据其他数据获取DT的子集。表:

indexTable = data.table(Season = c("Winter","Spring","Spring"),
                        Station = c("B","B","A"),
                        Group = c("1","2","3"))

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

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索引表示法获取expectedTable(例如使用setkey?)

1 个答案:

答案 0 :(得分:4)

你在两张桌子的内部联接之后。

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

参考