remove the first row for each group

前端 未结 4 1249
庸人自扰
庸人自扰 2021-01-14 06:25

suppose I have a dataset like this

df <- data.frame(group = c(rep(1,3),rep(2,2), rep(3,2),rep(4,3),rep(5, 2)), score = c(30, 10, 22, 44, 6, 5, 20, 35, 2,         


        
4条回答
  •  粉色の甜心
    2021-01-14 07:14

    dplyr::filter(df, group == lag(group))
       group score
    1     1    10
    2     1    22
    3     2     6
    4     3    20
    5     4     2
    6     4    60
    7     5     5
    

    See lead and lag of package dplyr for more information:

    https://dplyr.tidyverse.org/reference/lead-lag.html

提交回复
热议问题