[R] Code working but too slow, any idea for how to speed it up ?(no loop in it)

anna lippelanna24 at hotmail.com
Fri Feb 12 19:29:14 CET 2010

Hello my friends, 
here is a code I wrote with no loops on matrix that is taking too long (2
seconds and I call him 720 times --> 12 minutes):

mat1 and mat2 are both matrix with 103 columns and 164 rows.

sequence <- matrix(seq(1 : ncol(mat1)))

returns <- apply(sequence, 1, function, mat1= mat1, mat2 = mat2, day = 1)

function<- function(mat1, mat2, colNb, day){           
    mat1<- matrix(mat1[, colNb] )
    mat2<- matrix(mat2[, colNb])
    nbDays <- length(mat1)       
    returns <- abs(mat1[1:(nbDays - day)]) * ((as.ts(lag(mat2,
day))/as.ts(mat2))^mat1[1:(nbDays - day)] - 1)

I am wondering if the fact of using lag and ts is not slowering down the
code, any idea?

Anna Lippel
View this message in context: http://n4.nabble.com/Code-working-but-too-slow-any-idea-for-how-to-speed-it-up-no-loop-in-it-tp1483110p1483110.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list