Maximum of remaining elements of vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Maximum of remaining elements of vector

behave14
This post was updated on .
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

Of course this can be done with a loop or apply, but isn't there a nicer and faster way to do so?

Regards,
bahave14
Reply | Threaded
Open this post in threaded view
|

Re: Maximum of remaining elements of vector

David Carlson
> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of behave14
> Sent: Monday, March 12, 2012 11:15 AM
> To: [hidden email]
> Subject: [R] Maximum of remaining elements of vector
>
> 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
>
> Of course this can be done with a loop, but isn't there a nicer and
> faster
> way to do so?


f <- function(x) sapply(1:length(x), function(y) max(x[y:length(x)]))
f(x)

>
> Regards,
> bahave14
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Maximum-of-
> remaining-elements-of-vector-tp4466499p4466499.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.


----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Maximum of remaining elements of vector

Petr Savicky
In reply to this post by behave14
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.

______________________________________________
[hidden email] 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.