例如,我有一个ID列表和一个开始日期列,这些ID第一次产生价值.我们有与开始日期相对应的MonthDuration.(例如对于ID%1,开始日期是2024-01-01,它将始终是等于1的MonthDuration.
ID <- c("1","1","1","2","2")
StartDate <- c('2024-01-01', '2024-01-01', '2024-01-01', '2024-02-01','2024-02-01')
MonthDuration<- c("1","2","3","1","2")
Value <- c("153","203","391","444","212")
df <- data.frame(ID,StartDate,MonthDuration,Value)
ID StartDate MonthDuration Value
1 2024-01-01 1 153
1 2024-01-01 2 203
1 2024-01-01 3 391
2 2024-02-01 1 444
2 2024-02-01 2 212
我想要的是日期列匹配MonthDuration和StartDate,以生成基于这2列的日期,直到每个ID的最大MonthDuration数.
ID StartDate MonthDuration Value Date
1 2024-01-01 1 153 2024-01-01
1 2024-01-01 2 203 2024-02-01
1 2024-01-01 3 391 2024-03-01
2 2024-02-01 1 444 2024-02-01
2 2024-02-01 2 212 2024-03-01