[R] removing for-loop question

Jeff D. Hamann jeff.hamann at forestinformatics.com
Mon Aug 21 21:31:19 CEST 2006


R-loop-killers:

I'm looking for a hint to remove the loops from this little piece of code.

Since most of the of computing I deal with is very "loop-centric", it's
often difficult for me to translate index-based code into loop-free code.
Does anyone have any hints for speeding this up?

The actuals is a data.frame object and the resids is a simple matrix...

  cb <- cov( rbind( actuals$tvolh, resids[,5] ) )
  covb <- 0
  for( i in 1:nrow( actuals ) ) {
    for( j in 2:nrow( actuals ) ) {
      covb <- covb + actuals[i,]$ha * block.acts[j,]$ha * cb[i,j]
    }
  }


thanks,
Jeff.


-- 
Forest Informatics, Inc.
PO Box 1421
Corvallis, Oregon 97339-1421



More information about the R-help mailing list