我有这样的数据
| ID | Value | Condition
| ---|-------| ---------
| 01 | NA | Start_1
| NA | 1 | NA
| NA | 1 | NA
| 04 | NA | Start_2
| 14 | 4 | NA
| 15 | 4 | NA
| 16 | NA | End_2
| 17 | NA | Start_3
| NA | 4 | NA
| 27 | NA | End_3
| 28 | 5 | NA
| 29 | NA | End_1
我希望聚合Start与其相关End之间的值。
更准确地说,它应该是从一个开始 Start_x 到其相关的 End_x <之间的值之和/ em>或者如果还有一个 Start_y 进入 End_x ,那么 Start_y 中的值直到 End_y (或直到另一个 Start_z )与 Start_y 相关。此外,还应列出相关的ID。
所以上面数据的解决方案应该是这样的:
Start_1: Sum of Values: 7; IDs: 01,NA,28,29
Start_2: Sum of Values: 8; IDs: 04,14,15,16
Start_3: Sum of Values: 4; IDs: 17,NA,27
在非基于矢量的编程语言中,我将定义一个循环函数
**for** all rows:
**if** there is a start do ...
**if** there is a end do ...
**if** there is a NA do ...
但这不是R的有效解决方案,因为它适用于矢量化。那么你有什么提示可以解决更有效的问题吗?
提前致谢!