在R中用data.table计算两个日期之间的累计和
Calculate cumulative sum between two dates with a data.table in R
我有一个 data.table
形状如下:
date_from date_until value
2015-01-01 2015-01-03 100
2015-01-02 2015-01-05 50
2015-01-02 2015-01-04 10
...
我想做的是:我想计算一年中每个日期的累计和。对于第一行,值 100 与从 2015-01-01 到 2015-01-03 的每一天相关。我想添加与特定日期相关的所有值。
所以,最后会有这样的data.table
:
date value
2015-01-01 100
2015-01-02 160
2015-01-03 160
2015-01-04 60
2015-01-05 50
data.table
有什么简单的方法可以做到这一点吗?
dt[, .(date = seq(as.Date(date_from, '%Y-%m-%d'),
as.Date(date_until, '%Y-%m-%d'),
by='1 day'),
value), by = 1:nrow(dt)][, sum(value), by = date]
# date V1
#1: 2015-01-01 100
#2: 2015-01-02 160
#3: 2015-01-03 160
#4: 2015-01-04 60
#5: 2015-01-05 50
另一个使用 foverlaps
的选项:
# convert to Date for ease
dt[, date_from := as.Date(date_from, '%Y-%m-%d')]
dt[, date_until := as.Date(date_until, '%Y-%m-%d')]
# all of the dates
alldates = dt[, do.call(seq, c(as.list(range(c(date_from, date_until))), by = '1 day'))]
# foverlaps to find the intersections
foverlaps(dt, data.table(date_from = alldates, date_until = alldates,
key = c('date_from', 'date_until')))[,
sum(value), by = date_from]
# date_from V1
#1: 2015-01-01 100
#2: 2015-01-02 160
#3: 2015-01-03 160
#4: 2015-01-04 60
#5: 2015-01-05 50
我有一个 data.table
形状如下:
date_from date_until value
2015-01-01 2015-01-03 100
2015-01-02 2015-01-05 50
2015-01-02 2015-01-04 10
...
我想做的是:我想计算一年中每个日期的累计和。对于第一行,值 100 与从 2015-01-01 到 2015-01-03 的每一天相关。我想添加与特定日期相关的所有值。
所以,最后会有这样的data.table
:
date value
2015-01-01 100
2015-01-02 160
2015-01-03 160
2015-01-04 60
2015-01-05 50
data.table
有什么简单的方法可以做到这一点吗?
dt[, .(date = seq(as.Date(date_from, '%Y-%m-%d'),
as.Date(date_until, '%Y-%m-%d'),
by='1 day'),
value), by = 1:nrow(dt)][, sum(value), by = date]
# date V1
#1: 2015-01-01 100
#2: 2015-01-02 160
#3: 2015-01-03 160
#4: 2015-01-04 60
#5: 2015-01-05 50
另一个使用 foverlaps
的选项:
# convert to Date for ease
dt[, date_from := as.Date(date_from, '%Y-%m-%d')]
dt[, date_until := as.Date(date_until, '%Y-%m-%d')]
# all of the dates
alldates = dt[, do.call(seq, c(as.list(range(c(date_from, date_until))), by = '1 day'))]
# foverlaps to find the intersections
foverlaps(dt, data.table(date_from = alldates, date_until = alldates,
key = c('date_from', 'date_until')))[,
sum(value), by = date_from]
# date_from V1
#1: 2015-01-01 100
#2: 2015-01-02 160
#3: 2015-01-03 160
#4: 2015-01-04 60
#5: 2015-01-05 50