We can try with rleidand duplicated. We create run-length identifiers with rleid(from data.table), so that only adjacent elements that are equal form one group, get a logical index of values duplicatedand a subset of the vector.
library(data.table)
v1[!duplicated(rleid(v1))]
#[1] 3 9 2 3 7
Or, as indicated in the OP, we can use rlefrom base Rand extract values.
rle(v1)$values
data
v1 <- c(3,3,9,9,2,2,3,3,7,7)
akrun source
share