

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


On 5/24/2010 6:02 AM, 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)
a < c(1,2,2,2,2,3,3,3,4,4,4,5,5,6,7,7,7)
rle(a)
Run Length Encoding
lengths: int [1:7] 1 4 3 3 2 1 3
values : num [1:7] 1 2 3 4 5 6 7
cumsum(head(rle(a)$lengths, 1)) + 1
[1] 2 6 9 12 14 15
?rle
> 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

Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 8454495 (Tu, Th)
tel: (732) 5120171 (M, W, F)
fax: (917) 4380894
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Hi,
is this what you need?
b < c(NA, a[1:length(a)1]) # shift values of a one step to the right
which(ab == 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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Thank you !
Great answers...now it seems very easy...
As usual...a the obviousness of a solution depends on how you face the problem...
Thank you for help me in find the good approaches...


Try this:
> which(diff(a) > 0) + 1
[1] 2 6 9 12 14 15
On Mon, May 24, 2010 at 6:02 AM, speretti < [hidden email]> 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
> 
> View this message in context: http://r.789695.n4.nabble.com/switchingelementsofavectortp2228373p2228373.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


And, just for kicks, a yet another (somewhat inefficient)solution:
match(unique(a),a)
Cheers,
Bert Gunter
Genentech Nonclinical Statistics
Original Message
From: [hidden email] [mailto: [hidden email]] On
Behalf Of speretti
Sent: Monday, May 24, 2010 3:03 AM
To: [hidden email]
Subject: [R] switching elements of a vector
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

View this message in context:
http://r.789695.n4.nabble.com/switchingelementsofavectortp2228373p2228373.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

