首页 > 解决方案 > 为 R 中的某些列条目创建新列

问题描述

想象一下以下数据框的片段:

       ID        ActivityName     Time         Type    Shape 
1       1             Request    0.000       Type_1      767           
2       1             Request  600.000       Type_1      767           
3       1               Start  600.000       Type_1     1376           
4       1               Start  600.000       Type_1     1376  
5       1               Delay 2962.295       Type_1     1022         
6       1 Schedule Activities  600.000       Type_1       15           
7       1 Schedule Activities 2062.295       Type_1       15  

我想要做的是根据ActivityName.

具体来说,我想将同一活动的两个后续行组合成一行,并带有开始和结束时间戳(从 开始Time,以秒为单位)。

鉴于并非所有条目ActivityName都有匹配的第二个条目(但是,最多两个连续条目是相同的),我还想删除这样的行,例如Delay在这种情况下。

然后它应该看起来像:

       ID        ActivityName  StartTime   EndTime      Type    Shape 
1       1             Request  0.000       600.000      Type_1  767           
2       1               Start  600.000     600.000      Type_1  1375
3       1 Schedule Activities  600.000     2062.295     Type_1  15

中的所有类别在ActivityName该列中出现多次。我不希望Time在整个列中比较它们的关联,而是只比较那些连续出现两次相同的情况。

任何帮助将不胜感激。

标签: rdplyrpivotdata-manipulation

解决方案


推荐阅读