Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Calculate difference between values in rows with group reference

Tags:

r

This is my df:

group value
1     10
1     20
1     25
2     5
2     10
2     15 

I now want to compute differences between each value of a group and a reference value, which is the first row of a group. More precisely:

group value diff
1     10    NA # because this is the reference for group 1
1     20    10 # value[2] - value[1]
1     25    15 # value[3] - value[1]
2     5     NA # because this is the reference for group 2
2     10    5  # value[5] - value[4]
2     15    10 # value[6] - value[4]

I found good answers for difference scores of the previous line (e.g., lag-function in dpylr, shift-function in data.table). However, I am looking for a fixed reference point and I couldn't make it work.

like image 550
diggi2395 Avatar asked Nov 26 '25 12:11

diggi2395


1 Answers

I think you can also use this:

library(dplyr)
df %>%
  group_by(group) %>%
  mutate(diff = value - value[1], 
         diff = replace(diff, row_number() == 1, NA))

# A tibble: 6 x 3
# Groups:   group [2]
  group value  diff
  <int> <int> <int>
1     1    10    NA
2     1    20    10
3     1    25    15
4     2     5    NA
5     2    10     5
6     2    15    10
like image 127
Anoushiravan R Avatar answered Nov 30 '25 20:11

Anoushiravan R