How to find if two or more continuously elements of a vector are equal in R
05:58 25 Aug 2018

I want to find a way to determine if two or more continuously elements of a vector are equal.

For example, in vector x=c(1,1,1,2,3,1,3), the first, the second and the third element are equal.

With the following command, I can determine if a vector, say y, contains two or more continuously elements that are equal to 2 or 3

all(rle(y)$lengths[which( rle(y)$values==2 | rle(y)$values==3 )]==1)

Is there any other faster way?

EDIT

Let say we have the vector z=c(1,1,2,1,2,2,3,2,3,3). I want a vector with three elements as output. The first element will refer to value 1, the second to 2 and the third one to 3. The values of the elements of the output vector will be equal to 1 if two or more continuously elements of z are the same for one value of 1,2,3 and 0 otherwise. So, the output for the vector z will be (1,1,1).

For the vector w=c(1,1,2,3,2,3,1) the output will be 1,0,0, since only for the value 1 there are two continuously elements, that is in the first and in the second position of w.

r vector equality