Removing Only Adjacent Duplicates in Data Frame in R

前端 未结 3 757
南笙
南笙 2021-01-12 21:20

I have a data frame in R that is supposed to have duplicates. However, there are some duplicates that I would need to remove. In particular, I only want to

3条回答
  •  广开言路
    2021-01-12 21:30

    You could also try

    df[c(diff(as.numeric(df$x)), 1) != 0, ]
    

    In case x is of character class (rather than factor), try

    df[c(diff(as.numeric(factor(df$x))), 1) != 0, ]
    #    x  y
    # 1  A  1
    # 2  B  2
    # 3  C  3
    # 4  A  4
    # 5  B  5
    # 6  C  6
    # 7  A  7
    # 9  B  9
    # 10 C 10
    

提交回复
热议问题