【发布时间】:2021-07-20 23:37:32
【问题描述】:
我有两个数据框:数据框 1,它是具有分类值的不规则时间间隔数据,以及数据框 2,它是具有整数值的规则间隔数据。
数据框 1
| Start Date | End Date | Category |
|---|---|---|
| 1980-01-05 | 1983-02-17 | A |
| 1983-02-17 | 1987-01-02 | B |
| 1987-01-02 | 1989-11-10 | C |
| 1989-11-10 | 1992-03-20 | D |
数据框 2
| Date | variable 1 | variable 2 | variable 3 | ... |
|---|---|---|---|---|
| 1980-01-01 | 0 | 0 | 2 | ... |
| 1980-02-01 | 0 | 0 | 0 | ... |
| 1980-03-01 | 0 | 0 | 0 | ... |
| 1980-04-01 | 0 | 1 | 2 | ... |
| 1980-05-01 | 0 | 1 | 0 | ... |
| 1980-06-01 | -1 | 0 | 1 | ... |
| 1980-07-01 | -2 | 0 | 1 | ... |
| 1980-08-01 | -1 | 0 | 2 | ... |
| 1980-09-01 | 0 | 2 | 1 | ... |
| 1980-10-01 | 0 | 0 | 2 | ... |
| ... | ... | ... | ... | ... |
使用此数据框,我想将数据框 2 中的观察结果合并到数据框 1 中,将数据框 1 中开始日期到结束日期区间的值相加。
所以输出应该是这样的:
| Start Date | End Date | Category | variable 1 | variable 2 | variable 3 | ... |
|---|---|---|---|---|---|---|
| 1980-01-05 | 1983-02-17 | A | Sum of variable 1 from the start date to end date | Sum of variable 2 from the start date to end date | Sum of variable 3 from the start date to end date | ... |
| 1983-02-17 | 1987-01-02 | B | Sum of variable 1 from the start date to end date | Sum of variable 2 from the start date to end date | Sum of variable 3 from the start date to end date | ... |
| 1987-01-02 | 1989-11-10 | C | Sum of variable 1 from the start date to end date | Sum of variable 2 from the start date to end date | Sum of variable 3 from the start date to end date | ... |
| 1989-11-10 | 1992-03-20 | D | Sum of variable 1 from the start date to end date | Sum of variable 2 from the start date to end date | Sum of variable 3 from the start date to end date | ... |
【问题讨论】:
-
是否始终保证结束日期行 n = 开始日期行 n+1?
-
是的。始终保证如此。 @dash2
标签: r time merge sum intervals