Skip to content
Prev 98012 / 398498 Next

How to remove similar successive objects from a vector?

Is there some (much) more efficient way to do this?

VECTOR=c(3,2,4,5,5,3,3,5,1,6,6);
NEWVECTOR=VECTOR[1];

for(i in 1:(length(VECTOR)-1))
{
	if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){
		NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])}
}
[1] 3 2 4 5 5 3 3 5 1 6 6
[1] 3 2 4 5 3 5 1 6

_______________________________
Atte Tenkanen
University of Turku, Finland