Time Series - Create first differences by group

library(tidyverse)
library(kableExtra)

df <- data.frame(
 'time' = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
 'id' = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
 'score' = c(rnorm(9, 10, 2))
)

df <- df %>% 
  group_by(id) %>% mutate(lagscore = lag(score)) %>% 
  mutate(diffscore = score - lagscore)

df %>% kable() %>% kable_styling()
time id score lagscore diffscore
1 1 8.381000 NA NA
2 1 9.016563 8.381000 0.6355627
3 1 10.187045 9.016563 1.1704821
1 2 8.388847 NA NA
2 2 12.958878 8.388847 4.5700310
3 2 13.313880 12.958878 0.3550016
1 3 13.206961 NA NA
2 3 10.130180 13.206961 -3.0767809
3 3 8.783671 10.130180 -1.3465091

Bo\(^2\)m =)