Hello I have the following dataset:
year<-c(0,1,2,3,4,5,6,7,8)
total<-c(4462,8233,8233,8631,8631,9016,9016,9420,9494)
df<-data.frame(year,total)
I need to compute a third column from the difference of n and n-1 elements in the second column:
new<-c(4462,3771,0,398,0,385,0,404,74)
So that the result for the first element is for instance 8233-4462 = 3771; second element 8233 - 8233 = 0, etc.
How could I do this? The final result should be:
df<-data.frame(year,total,new)