remove the first row for each group

前端 未结 4 1244
庸人自扰
庸人自扰 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:19

    Another base R option would be to check the adjacent elements

    df[c(FALSE,df$group[-1]==df$group[-nrow(df)]),]
    #   group score
    #2      1    10
    #3      1    22
    #5      2     6
    #7      3    20
    #9      4     2
    #10     4    60
    #12     5     5
    

    Here I removed the first observation in 'group' (df$group[-1]) and compared (==) with the vector in which last observation is removed (df$group[-nrow(df)])). As the length of the comparison is one less than the nrow of the dataset, we pad with FALSE at the top and use this as logical index to subset the dataset.

提交回复
热议问题