[R] How to remove similar successive objects from a vector?

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Aug 16 09:57:03 CEST 2006

```try something like the following:

x <- c(3,3,2,4,5,5,3,3,5,1,6,6)
#########
nx <- length(x)
ind <- c(TRUE, (x[1:(nx-1)] - x[2:nx]) != 0)
x[ind]

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

----- Original Message -----
From: "Atte Tenkanen" <attenka at utu.fi>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, August 16, 2006 9:42 AM
Subject: [R] 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])}
> }
>
>> VECTOR
> [1] 3 2 4 5 5 3 3 5 1 6 6
>> NEWVECTOR
> [1] 3 2 4 5 3 5 1 6
>
> _______________________________
> Atte Tenkanen
> University of Turku, Finland
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help