dplyr: lead() and lag() wrong when used with group_by()

后端 未结 3 1725
旧巷少年郎
旧巷少年郎 2020-12-02 09:46

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         


        
3条回答
  •  天命终不由人
    2020-12-02 10:18

    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.

提交回复
热议问题