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

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Wed 16 Aug 2006 - 18:05:48 EST

On Wed, 2006-08-16 at 10:42 +0300, Atte Tenkanen wrote:
> 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

Is this what you mean?

x <- c(3, 2, 4, 5, 5, 3, 3, 5, 1, 6, 6)
x.wanted <- c(3, 2, 4, 5, 3, 5, 1, 6)

X <- x[diff(x) != 0]

all.equal(x.wanted, X) # check it works

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 *Note new Address and Fax and Telephone numbers from 10th April 2006*
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/cv/
WC1E 6BT                          [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed Aug 16 18:21:52 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 16 Aug 2006 - 20:21:32 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.