I have a dataframe with a start and end date for each row. I would like to calculate the number of days between the two dates and split it by year. So going from this:
id <- c(1,2,3)
start <- as.Date(c('01/01/2015','01/01/2016','07/01/2015'), format = '%m/%d/%Y')
end <- as.Date(c('12/31/2016','12/31/2016','12/31/2016'), format = '%m/%d/%Y')
df <- data.frame(id, start, end)
id | Start | End |
---|---|---|
1 | 01/01/2015 | 12/31/2016 |
2 | 01/01/2016 | 12/31/2016 |
3 | 01/07/2015 | 12/31/2016 |
To this:
id | Start | End | days_no. | year_2015 | year_2016 |
---|---|---|---|---|---|
1 | 01/01/2015 | 12/31/2016 | 730 | 365 | 365 |
2 | 01/01/2016 | 12/31/2016 | 365 | 0 | 365 |
3 | 07/01/2015 | 12/31/2016 | 548 | 183 | 365 |
Any help is appreciated, please note i would like to compute the yearly stats dynamically, i may end up with many year columns in my practical case... I'm guessing lubridate may help but i'm unsure where to start.