Hello my dataframe looks like this for a very large number of years and products:
product<-c(1,2,3)
yr1<-c(109,213,30)
yr2<-c(613,488,125)
yr3<-c(729,494,127)
df<-data.frame(product,yr1,yr2,yr3)
I need to transform it so that the values for the years after the first are the difference of the current value and the previous (lag) value, so that it looks like this:
yr2<-c(504,275,95)
yr3<-c(116,6,2)
df<-data.frame(product,yr1,yr2,yr3)
With 613-109=504, 729-613=116, etc.
How can I do this?