I am trying to created a 7 day lag difference by group. So, I am trying to replicate the code below and hope to get a similar result with a 7 day lag instead.
library(dplyr)
dat %>% mutate(dx=c(NA, diff(x)), dy=c(NA, diff(y)))
x y dx dy
1 5 3 NA NA
2 8 9 3 6
3 3 1 -5 -8
4 1 5 -2 4
But I am getting the error messages:
Error: incompatible size (900), expecting 905 (the group size) or 1
is there a quick and easy way to fix this error. I undertstand it might have to do with mutate