Mittwoch, 2. April 2014

Unique List, but with order

How to create a list of elements in order:

(x <- structure(c(3L, 3L, 3L, 3L, 1L,  2L, 2L,
            2L, 2L, 3L, 2L, 1L, 1L, 2L, 2L,
            2L, 2L, 2L, 2L, 2L, 2L, 2L),
            .Label = c("B", "C", "D"), class = "factor"))

[1] D D D D B C C C C D C B B C C C C C C C C C
Levels: B C D


> x[c(TRUE, diff(as.numeric(x))!=0)]
[1] D B C D C B C
Levels: B C D