[R] switching elements of a vector

schuster mail at friedrich-schuster.de
Mon May 24 12:33:46 CEST 2010


Hi, 
is this what you need?

b <- c(NA, a[1:length(a)-1])   # shift values of a one step to the right
which(a-b == 1)


On Monday 24 May 2010 12:02:55 pm speretti wrote:
> Hi,
> 
> I would like to receive help for the following matter:
> 
> If I'm dealing with a numeric vectors containing increasing elements.
> i.e.
> 
> a<-c(1,2,2,2,2,3,3,3,4,4,4,5,5,6,7,7,7)
> 
> There exist an efficient way to obtain an vector that indicates the
>  position of the changing element of "a"?
> In this case it would be something like:
> 
> index<-c(1,6,9,12,14,15)
> 
> usually I'm used cycles to obtain boolean vectors of the same length of "a"
> indicating the changing elements ...later I've muliplied them for their
> numeric sequence and after that I've selected elements different from zero
> ...it is quite long...
> can you find an easier solution?
> 
> Thank you for you help
> 

-- 
----
Friedrich Schuster
Dompfaffenweg 6
69123 Heidelberg



More information about the R-help mailing list