Suppose I have the following example dataframe with each id observation containing 1 group 1 observation (reference value):
id date group
1 15-01-2022 1
1 15-01-2022 2
1 16-01-2022 2
1 20-01-2022 2
2 18-01-2022 1
2 20-01-2022 2
2 27-01-2022 2
I want to calculate a column for the difference between each date for ids in group 1 vs group 2:
id date group diff
1 15-01-2022 1 NA
1 15-01-2022 2 0
1 16-01-2022 2 1
1 20-01-2022 2 5
2 18-01-2022 1 NA
2 20-01-2022 2 2
2 27-01-2022 2 9