我有一个dataframe df
,我需要创建一个新的列delay
,当列TEXT
等于特定字符串时,计算列TIME
的特定行之间的差异:P_S1 - TEXT_W1之间的差异;P_S2 - TEXT_W2。在这个例子中只有两个差异,但在真实的的数据集中有更多的差异。
请参见下面的示例:
| ID | TEXT | TIME | delay |
| P48_1_st | TEXT_W1[1] | 1669 | NA |
| P48_1_st | TEXT_W1 | 1669 | NA |
| P48_1_st | P_S1 | 1748 | 79 |
| P48_1_st | TEXT_W1[2] | 2377 | NA |
| P48_1_st | TEXT_W2[1] | 3091 | NA |
| P48_1_st | TEXT_W2 | 3093 | NA |
| P48_1_st | P_S2 | 3156 | 63 |
| P48_1_st | TEXT_W2[2] | 3562 | NA |
| P48_1_st | TEXT_W2[1] | 8237 | NA |
| P48_1_st | A_P_S2 | 8319 | NA |
| P48_1_st | TEXT_W2[5] | 8379 | NA |
| P48_1_st | TEXT_W1[1] | 8380 | NA |
| P48_1_st | A_P_S1 | 8447 | NA |
| P48_1_st | TEXT_W1[3] | 8507 | NA |
| P48_1_st | TEXT_W1[1] | 8700 | NA |
| P48_1_st | P_S1[2] | 8788 | NA |
| P48_1_st | TEXT_W1[6] | 8848 | NA |
| P48_1_st | TEXT_W2 | 9144 | NA |
| P48_1_st | TEXT_W2 | 9168 | NA |
| P48_1_st | TEXT_W2[1] | 9282 | NA |
| P48_1_st | P_S2[2] | 9343 | NA |
| P48_1_st | TEXT_W2[6] | 9403 | NA |
| P48_1_st | TEXT_W2[1] | 9441 | NA |
| P48_1_st | TEXT_W2[4] | 9441 | NA |
| P48_1_st | TEXT_W2[1] | 9443 | NA |
| P48_1_st | P_S2[2] | 9513 | NA |
| P48_1_st | TEXT_W2[6] | 9573 | NA |
| P48_2_st | TEXT_W1[1] | 6148 | NA |
| P48_2_st | TEXT_W1 | 6154 | NA |
| P48_2_st | P_S1 | 6234 | 80 |
| P48_2_st | TEXT_W1[2] | 6689 | NA |
| P48_2_st | TEXT_W2[1] | 6911 | NA |
| P48_2_st | TEXT_W2 | 6915 | NA |
| P48_2_st | P_S2 | 7002 | 87 |
| P48_2_st | TEXT_W2[2] | 7210 | NA |
| P48_2_st | TEXT_W2[1] | 10602 | NA |
| P48_2_st | A_P_S2 | 10670 | NA |
| P48_2_st | TEXT_W2[5] | 10731 | NA |
| P48_2_st | TEXT_W1 | 11250 | NA |
| P48_2_st | TEXT_W1 | 11251 | NA |
| P48_2_st | TEXT_W1 | 11252 | NA |
| P48_2_st | TEXT_W1 | 11252 | NA |
| P48_2_st | TEXT_W1 | 11264 | NA |
| P48_2_st | TEXT_W1 | 11265 | NA |
| P48_2_st | TEXT_W1[1] | 11266 | NA |
| P48_2_st | A_P_S1 | 11353 | NA |
| P48_2_st | TEXT_W1[3] | 11514 | NA |
| P48_2_st | TEXT_W2[1] | 11775 | NA |
| P48_2_st | P_S2[2] | 11865 | NA |
| P48_2_st | TEXT_W2[6] | 12052 | NA |
2条答案
按热度按时间i86rm4rw1#
试试看
数据
drkbr07n2#
尝试:
我假设在有多个TEXT_W值的情况下,您希望使用最小值。如果不是这种情况,请替换
min
函数调用。例如,请参阅组P48_2_st x P_S1。此解决方案将独立于行的顺序工作。