[R] Maximum of remaining elements of vector

Petr Savicky savicky at cs.cas.cz
Mon Mar 12 18:07:31 CET 2012


On Mon, Mar 12, 2012 at 09:14:43AM -0700, behave14 wrote:
> Dear community
> 
> I have the following problem. I'd like to have the maximum of the remaining
> elements of a vector.
> 
> ex.
> x<-c(1,2,3,4,1,2,3,1,2,1) 
> 
> f(x) =  c(4,4,4,4,3,3,3,2,2,1)
> 
> where the first element is: max(x[1:length(x])
> the second one: max(x[2:length(x)])
> the third one: max(x[3:length(x)]) and so on

Hi.

Try this.

  rev(cummax(rev(x)))

  [1] 4 4 4 4 3 3 3 2 2 1

Hope this helps.

Petr Savicky.



More information about the R-help mailing list