[R] efficient rolling rank

Gabor Grothendieck ggrothendieck at gmail.com
Sat Apr 17 03:29:28 CEST 2010


I don't know if its any faster but you could try:

library(zoo)
z <- zoo(v)
rollapply(z, len, function(x) rank(x)[len])

On Fri, Apr 16, 2010 at 4:21 PM, zerdna <azege at yahoo.com> wrote:
>
> Could someone give me an idea on how to do rolling ranking, i.e. rank in the
> moving window of last 100 numbers in a long vector? I tried naive solution
> like
>
> roll.rank<-function(v, len){
>    r<-numeric(length(v)-len+1)
>    for(i in len:length(v))
>        r[i-len+1]<-rank(v[(i-len+1):i])[len]
>    r
>
> }
>
> However, it turns out pretty slow even on my rather able Linux box. For
> example, doing roll.rank(rnorm(50000), 100) takes 5 second, so for typical
> data i operate which is matrices of the size 1000 x 50000 i will need to
> wait 1.5 hours for one calculation. Does someone know a trick to properly do
> it quicker?
> --
> View this message in context: http://n4.nabble.com/efficient-rolling-rank-tp2013535p2013535.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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