【问题标题】:Conditional join in data.table?data.table 中的条件连接?
【发布时间】:2017-11-12 13:02:13
【问题描述】:

我有以下data.table,dtgrouped2

     MonthNo Unique Total
 1:       1    AAA    10
 2:       1    BBB     0
 3:       2    CCC     3
 4:       2    DDD     0
 5:       3    AAA     0
 6:       3    BBB    35
 7:       4    CCC    15
 8:       4    AAA     0
 9:       5    BBB    60
10:       5    CCC     0
11:       6    DDD   100
12:       6    AAA     0

还有一张桌子,dt2

     Unique1 StartDate EndDate Amount1 Amount2
1:     AAA        0        1       7       0
3:     AAA        1        2       5       0
2:     AAA        2        4       3       2

我想根据以下逻辑将 Amount1 和 Amount2 从 dt2 插入到 dtgrouped2 dtgrouped2 的每一行评估的“唯一”:

filter(StartDate< MonthNo & EndDate>=MonthNo)
then MAX(EndDate)
then insert Amount1 as Amount1 and Amount2 as Amount2

因此您可以看到结果因行而异。这将是预期的输出:

Date    MonthNo Unique  Items   Amounts Amount1 Amount2
Jan       1      AAA    x         10        7   0
Jan       1      BBB    y          2        NA  NA
Feb       2      CCC    x          3        NA  NA
Feb       2      DDD    y         15       NA   NA
March     3      AAA    y         20        3   2
March     3      BBB    x         35       NA   NA
April     4      CCC    x         15       NA   NA
April     4      AAA    y         50       3    2
May       5      BBB    x         60      NA    NA
May       5      CCC    y         70      NA    NA
June      6      DDD    x         100     NA    NA
June      6      AAA    y         20       NA   NA

【问题讨论】:

  • 我不明白你想要的输出。 1- Amounts 列来自哪里?它是如何填充的? (它不等于Total 列)。 2- 在第 5 行中,Amount1Amount2 的值 (?) 应分别为 50。 3 - 为什么在最后一行没有填充 Amount1Amount2 值?应该分别是32,不是吗?无论如何,我认为你可以使用这个:dt2[dtgrouped2, .(Amount1, Amount2), on = .(Unique1 = Unique, StartDate &lt; MonthNo, EndDate &lt;= MonthNo), mult = "last"](如果我理解正确的逻辑)。
  • 对不起,是的,你完全正确,逻辑应该是EndDate >=MonthNo。结果表只是未分组,这就是额外列的原因。谢谢,我试试这个!
  • 谢谢大卫,这成功了!非常感谢。

标签: r dataframe data.table


【解决方案1】:

我建议使用非 equi 连接与 mult = "last" 结合使用(以便仅捕获最近的 EndDate

dtgrouped2[, c("Amount1", "Amount2") := # Assign the below result to new columns in dtgrouped2
              dt2[dtgrouped2, # join
                  .(Amount1, Amount2), # get the column you need
                  on = .(Unique1 = Unique, # join conditions
                         StartDate < MonthNo, 
                         EndDate >= MonthNo), 
                  mult = "last"]] # get always the latest EndDate
dtgrouped2

#     MonthNo Unique Total Amount1 Amount2
#  1:       1    AAA    10       7       0
#  2:       1    BBB     0      NA      NA
#  3:       2    CCC     3      NA      NA
#  4:       2    DDD     0      NA      NA
#  5:       3    AAA     0       3       2
#  6:       3    BBB    35      NA      NA
#  7:       4    CCC    15      NA      NA
#  8:       4    AAA     0       3       2
#  9:       5    BBB    60      NA      NA
# 10:       5    CCC     0      NA      NA
# 11:       6    DDD   100      NA      NA
# 12:       6    AAA     0      NA      NA

您需要首先加入dt2[dtgrouped](而不是相反)的原因是因为您想为dtgrouped 中的每个可能值加入dt2,因此允许dt2 中的多个值加入dtgrouped

【讨论】:

  • 再次感谢大卫的精彩解释,这很有效!
猜你喜欢
  • 2015-04-29
  • 1970-01-01
  • 2022-12-18
  • 2016-11-12
  • 2013-10-03
  • 2017-07-01
  • 2014-03-02
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多