How to remove repeated elements in a vector, similar to 'set' in Python

前端 未结 3 2084
忘了有多久
忘了有多久 2021-01-31 14:21

I have a vector with repeated elements, and would like to remove them so that each element appears only once.

In Python I could construct a Set from a vector to achieve

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-31 15:23

    To remove contiguous duplicated elements only, you can compare the vector with a shifted version of itself:

    v <- c(1, 1, 5, 5, 5, 5, 2, 2, 6, 6, 1, 3, 3)
    v[c(TRUE, !v[-length(v)] == v[-1])]
    [1] 1 5 2 6 1 3
    

    The same can be written a little more elegantly using dplyr:

    library(dplyr)
    v[v != lag(v)]
    [1] NA  5  2  6  1  3
    

    The NA returned by lag() removes the first value, to keep the first value, you can change the default to a value that will be different from the first value.

    v[v != lag(v, default = !v[1])]
    [1] 1 5 2 6 1 3
    

提交回复
热议问题