This is a my df (data.frame):
df
group value 1 10 1 20 1 25 2 5 2 10 2 15
I need to calculate difference
try this with tapply
df$diff<-as.vector(unlist(tapply(df$value,df$group,FUN=function(x){ return (c(NA,diff(x)))})))