I want to find the lead() and lag() element in each group, but had some wrong results.
For example, data is like this:
library(dplyr)
df = data.frame
It may happen that stats::lag is used instead (e.g. when restoring environments with the session package). This can easly slip through unnoticed as it it won't throw an error when being used as in the question. Double-check by simply typing lag, use conflicted package, or disambiguate the function call by calling dplyr::lag instead.