[R] Fwd: Obtaing the maximum

arun smartpink111 at yahoo.com
Thu May 30 20:49:15 CEST 2013


Thanks Bill for the email.

Your function looks great.

f2<- function(x) x[c(diff(x)<1,TRUE)] 
 f2(a4)
#[1]  8  4  4  4 13 12
 f2(a3)
#[1]  8  1  1  1 13  4

Using the example you provided.
set.seed(24)  
 a5 <- sample(20, replace=TRUE, size=1e7)
 f1New<- function(x) x[c(diff(x)<0,TRUE)|c(x[-length(x)]==x[-1],TRUE)]
f3 <- function(x)x[c(which(diff(x)<1),length(x))] #Marion's function (changed `==` to `<`)
f1 <- function(x)  x[sort(c(which(c(diff(x)<0,TRUE)), which(x[-length(x)] == x[-1])))]
 identical(f1New(a5),f1(a5))
#[1] TRUE
 identical(f2(a5),f1(a5))
#[1] TRUE
 identical(f3(a5),f1(a5))
#[1] TRUE


system.time(f1New(a5))
#   user  system elapsed 
 # 1.884   0.212   2.102 
 system.time(f1(a5)) 
#   user  system elapsed 
 # 1.604   0.268   1.874 
 system.time(f2(a5))
#   user  system elapsed 
 # 1.080   0.120   1.206 
system.time(f3(a5)) #Marion's function
#   user  system elapsed 
 # 0.856   0.140   0.998 



A.K.



----- Original Message -----
From: William Dunlap <wdunlap at tibco.com>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Thursday, May 30, 2013 2:28 PM
Subject: RE: [R] Fwd:  Obtaing the maximum

Also, your suggestion could avoid the relatively expensive call to sort() if you used logical subscripts.
Note that, as a subscript, if conditionA and conditionB are logical vectors with the same length and
!any(conditionA & conditionB) then
   sort( c( which(conditionA), which(conditionB) ))
is equivalent to
   conditionA | conditionB
so your
   sort(c(which(c(diff(x)<0,TRUE)), which(x[-length(x)] == x[-1])))
is equivalent to
   c(diff(x)<0, TRUE) | c(x[-length(x)] == x[-1]), TRUE)
where I padded the second part with TRUE to match the lengths.
Now x[-length(x)]==x[-1] is the same as diff(x)==0 so you might as well use just
   c(diff(x) <= 0, TRUE)
    
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: arun [mailto:smartpink111 at yahoo.com]
> Sent: Thursday, May 30, 2013 11:13 AM
> To: William Dunlap
> Subject: Re: [R] Fwd: Obtaing the maximum
> 
> Thanks Bill.
> Arun
> 
> 
> 
> 
> ----- Original Message -----
> From: William Dunlap <wdunlap at tibco.com>
> To: arun <smartpink111 at yahoo.com>
> Cc:
> Sent: Thursday, May 30, 2013 2:07 PM
> Subject: RE: [R] Fwd:  Obtaing the maximum
> 
> I thought her != should have been a <, but since the original question was garbled it was
> hard to say.
> In any case, I was only pointing out that converting the logical subscripts to integer
> subscripts via
> which() and length() was not necessary.
> 
> By the way, solutions to problems like this are more easily compared if presented  as
> functions
> rather than complicated expressions.  E.g., instead of
>    a4[sort(c(which(c(diff(a4)<0,TRUE)), which(a4[-length(a4)] == a4[-1])))]
>    a3[sort(c(which(c(diff(a3)<0,TRUE)), which(a3[-length(a3)] == a3[-1])))]
> I would rather see
>    f1 <- function(x)  x[sort(c(which(c(diff(x)<0,TRUE)), which(x[-length(x)] == x[-1])))]
>    f1(a4)
>    f1(a3)
> Then others can quickly compare various algorithms on different datasets.  E.g.
>   f3 <- function(x)x[c(which(diff(x)<1),length(x))]
>   a4<-c(c(1,4,5,8),rep(4,3),c(1,8,11,13),8:12)
>   identical(f1(a4), f3(a4))
>   # [1] TRUE
>   identical(f1(a3), f3(a3))
>   # [1] TRUE
>   a5 <- sample(20, replace=TRUE, size=1e7)
>   identical(f1(a5), f3(a5))
>   # [1] TRUE
>   system.time(f1(a5))
>   # user  system elapsed
>   # 1.23    0.03    1.31
>   system.time(f3(a5))
>   # user  system elapsed
>   # 0.61    0.00    0.60
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
> 
> 
> > -----Original Message-----
> > From: arun [mailto:smartpink111 at yahoo.com]
> > Sent: Thursday, May 30, 2013 10:52 AM
> > To: William Dunlap
> > Subject: Re: [R] Fwd: Obtaing the maximum
> >
> > It works with a1.  (Already sent a reply to Marion.)
> >
> >
> > a3[c(diff(a3) != 1, TRUE)]
> > # [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
> >
> >
> > a4[sort(c(which(c(diff(a4)<0,TRUE)), which(a4[-length(a4)] == a4[-1])))]
> > #[1]  8  4  4  4 13 12
> > a4[c(diff(a4) != 1, TRUE)]
> > # [1]  1  5  8  4  4  4  1  8 11 13 12
> > A.K.
> >
> >
> > ----- Original Message -----
> > From: William Dunlap <wdunlap at tibco.com>
> > To: Marion Wenty <marion.wenty at kinderrechteinstitut.at>;
> > "tryingtolearnagain at gmail.com" <tryingtolearnagain at gmail.com>;
> > "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>; "r-help at r-project.org" <r-
> > help at r-project.org>
> > Cc:
> > Sent: Thursday, May 30, 2013 1:50 PM
> > Subject: RE: [R] Fwd:  Obtaing the maximum
> >
> > > 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))]
> >
> > Or, even shorter,
> >    a1[c(diff(a1) != 1, TRUE)]
> >
> > Bill Dunlap
> > Spotfire, TIBCO Software
> > wdunlap tibco.com
> >
> >
> > > -----Original Message-----
> > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf
> > > Of Marion Wenty
> > > Sent: Thursday, May 30, 2013 10:20 AM
> > > To: tryingtolearnagain at gmail.com; smartpink111 at yahoo.com; r-help at r-project.org
> > > Subject: [R] Fwd: 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
> > >
> > > ---------- Forwarded message ----------
> > > From: arun <smartpink111 at yahoo.com>
> > > Date: 2013/5/29
> > > Subject: Re: [R] Obtaing the maximum
> > > To: R help <r-help at r-project.org>
> > >
> > >
> > > 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.
> > >
> > >
> > >
> > > --
> > > Mag.a Marion Wenty
> > > Wissenschaftliche Mitarbeiterin
> > > Institut für Kinderrechte und Elternbildung
> > > Ballgasse 2, 6. Stock
> > > 1010 Wien
> > >
> > >     [[alternative HTML version deleted]]




More information about the R-help mailing list