edit: My question is similar to Calculate difference between values in consecutive rows by group, but for my purpose I need the first row to calculate the difference to 0 instead of returning NA.
I would like to add a new column to my dataframe that has the difference between the value of my "time" column in this row and the previous row.
Here is my sample data:
my_data = data.frame(time = c(.5, .75, 1, .25, .5, 1), group = rep(c("dogs","cats"), each=3))
which gives:
time group
1 0.50 dogs
2 0.75 dogs
3 1.00 dogs
4 0.25 cats
5 0.50 cats
6 1.00 cats
I now want a new variable called diff, that is computed separately for each group. When the row is the first row in each group, it should calculate the difference from 0.
time group diff
1 0.50 dogs .5
2 0.75 dogs .25
3 1.00 dogs .25
4 0.25 cats .25
5 0.50 cats .25
6 1.00 cats .50