Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find unique sequences in a vector? [duplicate]

Tags:

r

I want to find the unique sequences in my vector. A sequence is a series of identical values. If a sequence repeats, it counts as two sequences, as long as there is another sequence in between. A sequence can have a length of one value.

So that if my function is called findSequences(), it would work like this:

my_vector = c('a', 'a', 'b', 'a', 'c', 'c', 'b')

find_Sequences(my_vector)

> 'a', 'b', 'a', 'c', 'b'

unique() and distinct() don't do this.

like image 255
petyar Avatar asked Dec 03 '22 17:12

petyar


1 Answers

You can use rle.

rle(my_vector)$values
#[1] "a" "b" "a" "c" "b"
like image 142
GKi Avatar answered Dec 11 '22 15:12

GKi