Skip to content
Prev 179360 / 398506 Next

Relative subscripts?

Dear R-helpers,

I have a dataframe with several columns, one of which is called LENGTH.

I would like to make a new column called DIFF containing the value of
LENGTH minus LENGTH in the previous row, like this:

  ID LENGTH DIFF
1  1     10   NA
2  2     15    5
3  3     20    5
4  4     12   -8
5  5     18    6

I'd like to think there are "relative subscripts" in R but I can't
find any reference to such a thing.

Any help solving this problem would be much appreciated, thanks!

Mark Na