I have a df where each row is the cumulative sum of the row above it. Is there a way to derive the original values from this df?
X1 X2
1 1 5
2 3 9
3 6 12
4 10 14
5 15 15
Desired output:
X1 X2
1 1 5
2 2 4
3 3 3
4 4 2
5 5 1
Thanks
Just use
diff
. Assuming your dataset is called "mydf" and you want to do this for all columns, try:Since
diff
returns a vector oflength
one less than the input, you need to pad the input with a0
(thus also retaining the original value in that column).As @DavidArenburg mentions, you could also easily adapt this to "data.table" code too, like this: