[R] Obtaing the maximum

arun smartpink111 at yahoo.com
Thu May 30 19:45:21 CEST 2013



In cases like:
a3<-c(c(1,4,5,8),rep(1,3),c(1,8,11,13),1:4)
 a3[c(which(diff(a3)!=1),length(a3))]
# [1]  1  5  8  1  1  1  1  8 11 13  4
 a3[sort(c(which(c(diff(a3)<0,TRUE)), which(a3[-length(a3)] == a3[-1])))]
#[1]  8  1  1  1 13  4

#or
a4<-c(c(1,4,5,8),rep(4,3),c(1,8,11,13),8:12)
 a4[c(which(diff(a4)!=1),length(a4))]
#[1]  1  5  8  4  4  4  1  8 11 13 12
a4[sort(c(which(c(diff(a4)<0,TRUE)), which(a4[-length(a4)] == a4[-1])))]
#[1]  8  4  4  4 13 12

May be the OP's vector is similar to a1.

A.K.
________________________________
From: Marion Wenty <marion.wenty at gmx.at>
To: tryingtolearnagain at gmail.com; smartpink111 at yahoo.com; r-help at r-project.org 
Cc: R help <r-help at r-project.org> 
Sent: Thursday, May 30, 2013 1:06 PM
Subject: Re: [R] Obtaing the maximum




Hi,

or ... a shorter way would be:

a1<-c(1,2,3,4,5,1,2,3,1,1,1,2,3,4,5,1,2,3)

a1[c(which(diff(a1)!=1),length(a1))]

Hope it helps.

Marion


Gesendet: Mittwoch, 29. Mai 2013 um 07:04 Uhr
Von: arun <smartpink111 at yahoo.com>
An: "R help" <r-help at r-project.org>
Betreff: *** GMX Spamverdacht *** Re: [R] Obtaing the maximum
Hi,
May be this helps:
a1<-c(1:5,1:3,rep(1,2),1:5,1:3)
 a1
# [1] 1 2 3 4 5 1 2 3 1 1 1 2 3 4 5 1 2 3


a1[sort(c(which(c(diff(a1)<0,TRUE)), which(a1[-length(a1)] == a1[-1])))]
#[1] 5 3 1 1 5 3
a2<-c(1:2,rep(1,4),1:7,1:3)
a2
# [1] 1 2 1 1 1 1 1 2 3 4 5 6 7 1 2 3
 a2[sort(c(which(c(diff(a2)<0,TRUE)), which(a2[-length(a2)] == a2[-1])))]
#[1] 2 1 1 1 1 7 3

a3<-c(c(1,4,5,8),rep(1,3),c(1,8,11,13),1:4)
 a3
# [1]  1  4  5  8  1  1  1  1  8 11 13  1  2  3  4
 a3[sort(c(which(c(diff(a3)<0,TRUE)), which(a3[-length(a3)] == a3[-1])))]
#[1]  8  1  1  1 13  4

 a4<-c(c(1,4,5,8),rep(4,3),c(1,8,11,13),8:12)
 a4
# [1]  1  4  5  8  4  4  4  1  8 11 13  8  9 10 11 12
a4[sort(c(which(c(diff(a4)<0,TRUE)), which(a4[-length(a4)] == a4[-1])))]
#[1]  8  4  4  4 13 12
A.K.



>Hi all, I have a series whose tpe for that is like series I expose below.
>
>The thing is if a incremental number ends I´m in the first "type" of event
>in this success is named 5 (because of the maximum is 5). In the series I
>have this kind of events  5,3,1,1,5,3
>
>But I don´t know extactly a priori what is the maximum and when "stops"
>there is some function in r doing something similar this?
>
>Thans in advance to all.
>
>
>1
>2
>3
>4
>5
>1
>2
>3
>1
>1
>1
>2
>3
>4
>5
>1
>2
>3

______________________________________________
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.         



More information about the R-help mailing list