This is one way using base R
df$diff <- unlist(by(df$score , list(df$group) , function(i) c(NA,diff(i))))
or
df$diff <- ave(df$score , df$group , FUN=function(i) c(NA,diff(i)))
or using data.table - this will be more efficient for larger data.frames
library(data.table)
dt <- data.table(df)
setkey(dt,group)
dt[,diff:=c(NA,diff(score)),by=group]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…