Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove repeated numbers in a sequence

Tags:

r

duplicates

I have a vector of the type

c(3,3,...,9,9,...,2,2,...,3,3,...,7,7,...)

I want to remove the repeated numbers in a sequence, without breaking the order. This is, I would like to obtain something like

c(3,9,2,3,7,...)

How can I do this in R?

like image 473
Gillette Avatar asked Dec 01 '22 13:12

Gillette


1 Answers

We can also use the observation that a duplicate in sequence has a difference of 0 with their neighbour. Therefore, using base-R, we can do:

v[c(1,diff(v))!=0]
like image 131
Heroka Avatar answered Dec 15 '22 10:12

Heroka