I am trying to use the "ddply" function to calculate the difference in eery two consecutive rows representing two distinct years. The data set looks like this
year ID value
1 2005 A 10
2 2015 A 20
3 2005 B 25
4 2015 B 5
5 2005 C 10
6 2015 C 15
I am using the function as follows
ddply(df, "ID", function(x) (x[2,] - x[1,]))
However, it seems I am making an error in my function as the difference is calculated for the variables including the non-numeric ones. I get the following result.
year ID value
1 10 NA 10
2 10 NA -20
3 10 NA 5
I know the solution might be quite straight forward. I would wish to get the following summarised results.
ID change
A 10
B -5
C 5
Does anyone know how to achieve this using "ddply" or any other function?