Re: [R] Making routine faster by using apply instead of for-loop

2010-01-12 Thread Peter Ehlers
Your code is doing too many needless things. The following takes about one second on my slow Vista laptop. n <- 500 mat <- matrix(1:(n*n), n) v <- n:1 z <- 2*1:(n/2) w <- c(z, rev(z)) for(i in seq_len(n)){ for(j in seq_len(n)){ if(v[j] + w[i] <= n)(mat[i,j] <- NA) } } rownames(mat) <- v c

Re: [R] Making routine faster by using apply instead of for-loop

2010-01-12 Thread William Dunlap
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Etienne Stockhausen > Sent: Tuesday, January 12, 2010 10:59 AM > To: r-help@r-project.org > Subject: [R] Making routine faster by using apply instead of

[R] Making routine faster by using apply instead of for-loop

2010-01-12 Thread Etienne Stockhausen
Hey everybody, I have a small problem with a routine, which prepares some data for plotting. I've made a small example: c=10 mat=data.frame(matrix(1:(c*c),c,c)) row.names(mat)=seq(c,1,length=c) names(mat)=c(seq(2,c,length=c/2),seq(c,2,length=c/2)) v=as.numeric(row.names(mat)) w