我有一个数据帧,例如
Sp1 start end
A 100 1077
B 2316 4088
B 26647 28746
C 450 789
D 23 499
D 45999 60000
我想添加两个新的列,在这两个列中我累积添加了以前的开始和结束坐标
对于第一行,我应该始终获得相同的列:
Sp1 start end new_start new_end
A 100 1077 100 1077
然后对于第一个B:
Sp1 start end new_start new_end
A 100 1077 100 1077
B 2316 4088 1078 2850
-
where 1078 = 1077 +1
个 -
where 2850 = 1078+(4088-2316)
个
对于另一个B,它是名称:
Sp1 start end new_start new_end
A 100 1077 100 1077
B 2316 4088 1078 2850
B 26647 28746 2851 4950
- 其中
2851 = 2850 +1
- 其中
4950 = 4951 +(28746-26647)
对于另一个C,它是名称:
Sp1 start end new_start new_end
A 100 1077 100 1077
B 2316 4088 1078 2850
B 26647 28746 2851 4950
C 450 789 4951 5290
- 其中
4951 = 4950 +1
- 其中
5290 = 4951 +(789-450)
那么在最后,我应该会得到预期的结果:
Sp1 start end new_start new_end
A 100 1077 100 1077
B 2316 4088 1078 2850
B 26647 28746 2851 4950
C 450 789 4951 5290
如果有人有一个 idea ,那就太棒了!
以下是dput格式的数据帧,如果可以提供帮助的话:
structure(list(Sp1 = c("A", "B", "B", "C"), start = c(100L, 2316L,
26647L, 450L), end = c(1077, 4088, 28746, 789)), class = "data.frame", row.names = c(NA,
-4L))