I need to be able to find the differences between consecutive values in a column of a dataframe and to output these differences as a new column amended to the same dataframe.
Here's a head of a section of the data I am working with:
win0InitialChi2 win0FinalChi2 win1InitialChi2 win1FinalChi2 Det Bkgd CH4
1 764.0370000 0.000667857 2.19721e+03 4.24e-05 2.71747 1.92573
2 0.0483236 0.000655749 1.66682e-03 4.79e-05 2.71742 1.92492
3 0.0438540 0.000674729 9.08875e-04 5.63e-05 2.71709 1.91805
4 0.0452078 0.000677721 6.41243e-04 5.09e-05 2.71769 1.93004
5 0.0476242 0.000658611 5.76541e-04 3.99e-05 2.71747 1.92895
6 0.0425603 0.000667191 2.34680e-03 4.74e-05 2.71741 1.92236
I need to get the difference between consecutive values in the CH4 column, so entry 2 - entry 1, entry 3 - entry 2, and so on, and these to be output as a new column.
Edit: I have found how to do this using diff(), my next step is to find the percentage change. That is, what is 100*((entry 2 - entry 1) / entry 1). I am stuck at how to write a generalized operation for identifying the specific entry I need to divide by (in this case entry 1, but if I was to do 100*((entry 3 - entry 2) / entry 2) it would be entry 2, and so on. Thanks to anyone in advance.