简体   繁体   English

在 R 中运行求和时合并两个 data.tables

[英]Merging two data.tables while doing running sum in R

I have a data.table which represents the dividend payed by a company from 2019 to 2021.我有一个 data.table,它代表了一家公司从 2019 年到 2021 年支付的股息。

library(data.table)
div_dt <- structure(list(pay_date = structure(c(18885L, 18793L, 18701L, 
18611L, 18520L, 18428L, 18337L, 18246L, 18155L, 18064L, 17910L
), class = c("IDate", "Date")), cash_amount = c(0.09, 0.09, 0.09, 
0.09, 0.08, 0.07, 0.07, 0.05, 0.04, 0.04, 0.07)), row.names = c(NA, 
-11L), class = c("data.table", "data.frame"))

Below is a table showing all calendar days of this stock between 2019 to 2021.下表显示了该股票在 2019 年至 2021 年之间的所有日历日。

calendar_dt = data.table(current_date = seq(min(div_dt$pay_date), max(div_dt$pay_date), by="days"))

I want to show the sum of the last 4 quarters of dividends this stock has paid on any given date.我想显示该股票在任何给定日期支付的过去 4 个季度的股息总和。 To solve this I have added a new column div_start_date to calendar_dt , which shows the start date form which dividend has to be added to the given date current_date .为了解决这个问题,我在calendar_dt添加了一个新列div_start_date ,它显示了必须将股息添加到给定日期current_date的开始日期形式。

calendar_dt[, div_start_date := date - 365]

Can someone show me how to merge these tables so that for each calendar day in calendar_dt , the sum of dividends of the past 4 quarters is shown in a new column?有人可以告诉我如何合并这些表,以便在calendar_dt每个日历日,过去 4 个季度的股息总和显示在新列中吗?

this will work (not the most efficient join, but willl get the job done)这会起作用(不是最有效的加入,但会完成工作)

# set keys
setkey(calendar_dt, current_date)
setkey(div_dt, pay_date)
# join
calendar_dt[calendar_dt, 
            cast_last_365 := div_dt[pay_date %between% c(current_date - 365, current_date), 
                                    sum(cash_amount)],
            by = .EACHI]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM