[R] remove multiple elements from vector

baptiste auguie ba208 at exeter.ac.uk
Sat Sep 27 22:47:57 CEST 2008


Hi,

?unique should work fine for this.

I think the problem in your implementation is that you modify x in the  
loop, in particular its length may become shorter than the stopping  
condition.

HTH,

baptiste

On 27 Sep 2008, at 14:30, Bastian Offermann wrote:

> Hello all,
>
> one brief question
>
> I would like to remove double/triple elements from a vector, e.g.
>
> 0 1 1 1 1 2 2 2 4 5 6 6
>
> but keep one of these multiple ones. Should look like this finally:
>
> 0 1 2 4 5 6
>
> My function does not work somehow
>
> for(i in 2 : length(x)) {
>                                if(x[i]==x[i-1]) x <- x[-(i-1)]
>                            }
>
> any suggestion? Thanks.
>
> ______________________________________________
> R-help at r-project.org 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



More information about the R-help mailing list