[R] efficient rolling rank

zerdna azege at yahoo.com
Fri Apr 16 22:21:36 CEST 2010


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.



More information about the R-help mailing list