I have a dataframe like this:
ID s1 e1 s2 e2
A 50 150 80 180
A 160 350 280 470
A 355 700 800 1150
B 100 500 150 550
B 550 1500 800 1750
When the ID is identical I would like to calculate the difference between values in consecutive rows but different columns (for ID A: s1 in row2 minus e1 in row1; s1 in row3 minus e1 in row2; s2 in row2 minus e2 in row1; s2 in row3 minus e2 in row2) and add these values to a new column (diff1 and diff2).
The dataframe would then look like this:
ID s1 e1 s2 e2 diff1 diff2
A 50 150 80 180
A 160 350 280 470 10 100
A 355 700 800 1150 5 330
B 100 500 150 550
B 550 1500 800 1750 50 250
Is this possible?
Thank you in advance
WD