Remove consecutive duplicates from dataframe

后端 未结 3 501
孤城傲影
孤城傲影 2020-12-30 06:36

I have a data frame that I want to remove duplicates that are consecutive (in base). I know rle may be helpful here but can\'t think of how to use it. The exa

3条回答
  •  长发绾君心
    2020-12-30 07:00

    Here a fast solution using filter

    dat[(filter(dat,c(-1,1))!= 0)[,1],]
         v1   v2
    1     A  Jan
    3     E  May
    4     B  Feb
    7     A  Jan
    8     D  Apr
    10    A  Mar
    11    B  Feb
    12    E  May
    15    B  Feb
    18    C  Mar
    19    D  Apr
    NA  
    

    You need to add the last value of the original data to the result.

提交回复
热议问题