diff operation within a group, after a dplyr::group_by()

て烟熏妆下的殇ゞ 提交于 2019-11-30 18:25:53

Here is another solution using lag. Depending on the use case it might be more convenient than diff because the NAs clearly show that a particular value did not have predecessor whereas a 0 using diff might be the result of a) a missing predecessor or of b) the subtraction between two periods.

data %>% group_by(ID) %>% filter(n() > 1) %>%
  mutate(
    Difference = Score - lag(Score)
    )

#   ID Period Score Difference
# 1 123   2013   146         NA
# 2 123   2014   133        -13
# 3 456   2013   205         NA
# 4 456   2014   219         14
# 5 456   2015   140        -79
# 6  78   2012   192         NA
# 7  78   2013   199          7
# 8  78   2014   133        -66
# 9  78   2015   170         37
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!