简体   繁体   中英

Calculate cumulative sum between two dates with a data.table in R

I have a data.table with the following shape:

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
...

What I want to do is: I want to calculate for every date in the year the cumulative sum. For the first row the value 100 would be relevant for every day from 2015-01-01 until 2015-01-03. I want to add all values which are relevant for a certain date.

So, in the end there would be a data.table like this:

date        value
2015-01-01  100
2015-01-02  160
2015-01-03  160
2015-01-04   60
2015-01-05   50

Is there any easy way with the data.table to do this?

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

And another option using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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