I have a dataset where I want to group by year (and sum over days
), but if the number of days
for a certain date
is more than the number of days that have occurred in the year so far, the extra days should be added to the previous year. For example, below, out of the 153 days associated with 2019-02-01
, 31 of the days should go towards 2019 and 122 should go towards 2018.
The data looks like this:
date days
2018-02-01 0
2018-06-01 120
2018-07-01 30
2018-09-01 62
2019-02-01 153
2019-03-01 28
2019-04-01 31
And the desired output would look like this:
year days
2018 334
2019 90
How can I do this in R? (ideally using dplyr
or base-R)
The data can be recreated with:
date <- c(as.Date("2018-02-01"), as.Date("2018-06-01"), as.Date("2018-07-01"), as.Date("2018-09-01"), as.Date("2019-02-01"), as.Date("2019-03-01"), as.Date("2019-04-01"))
days <- c(0, 120, 30, 62, 153, 28, 31)
dat <- data.frame(date, days)