Remove consecutive duplicates from dataframe

后端 未结 3 506
孤城傲影
孤城傲影 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:09

    Using rle I came up with this

    ind <- cumsum(rle(as.character(dat$v1))$length)
    dat[ind, ]
    

    ind indicates either the first or the last of consecutive entries.

    EDIT:

    A simple solution to Matthews comment would be

    dat[15, 2] <- "May"
    dat[cumsum(rle(paste0(dat$v1, dat$v2))$length), ]
    

提交回复
热议问题