[R] How to remove similar successive objects from a vector?
Atte Tenkanen
attenka at utu.fi
Wed Aug 16 12:01:33 CEST 2006
Thanks for all respondents!
I wasn't precise enough, when I enclosed my example. In fact, I need a
version which works with all kinds of symbolic data, not only with
numbers. So these versions
rle(VECTOR)$values
and
VECTOR=c(3,2,2,3,4,4,5,5,5,3,3,3,5,1,6,6)
NEWVECTOR <- ifelse(VECTOR[-length(VECTOR)]==VECTOR[-1],NA,VECTOR)
NEWVECTOR[!is.na(NEWVECTOR)]
answered to my needs.
I made a test and the first version was 2.5x faster with my data, but
both works enough fast.
Atte
On Wed, 2006-08-16 at 08:58 +0100, Patrick Burns wrote:
> I think
>
> rle(VECTOR)$values
>
> will get you what you want.
>
> Patrick Burns
> patrick at burns-stat.com
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of S Poetry and "A Guide for the Unwilling S User")
>
> 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
> >University of Turku, Finland
> >
> >______________________________________________
> >R-help at 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.
> >
> >
> >
> >
More information about the R-help
mailing list