0

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
Esther
  • 441
  • 2
  • 15
  • 4
    its duplicate question with slightly difference. The solution which can work is: `my_data %>% group_by(group) %>% mutate(diff = abs(time - lag(time, default = 0)))` – MKR Mar 29 '18 at 21:47
  • this works, that you! – Esther Mar 29 '18 at 22:04
  • From one of the answer in the marked duplicate : `ave(my_data$time, my_data$group, FUN=function(x) c(x[1],diff(x)))` – Ronak Shah Mar 30 '18 at 01:20

0 Answers0