Chudar - 8 months ago 47

R Question

I am new to R. I have a data frame like following:

`group start end`

A 34 77

A 100 140

A 185 246

B 60 98

B 146 186

C 250 315

C 411 489

C 510 550

C 601 662

I would like to find the difference between the consecutive lines as follows:

`group start end`

A 78 99

A 141 184

B 99 145

C 316 410

C 490 509

C 551 600

Any little help would be highly appreciated. Thanks in advance

Answer Source

We can try with `data.table`

```
library(data.table)
setDT(df1)[,.(start = (start+end - start +1)[-.N],
end = (end +shift(start, type='lead')-end-1)[-.N] ) , by = group]
# group start end
#1: A 78 99
#2: A 141 184
#3: B 99 145
#4: C 316 410
#5: C 490 509
#6: C 551 600
```