# [R] Advice on Speed Improvement

From: Werner Wernersen <pensterfuzzer_at_yahoo.de>
Date: Mon 25 Apr 2005 - 03:05:27 EST

Hi folks!

Somehow I still write crappy code which is awefully slow. Maybe as a case study,
could anybody give me a hint on how to improve the following code for speed? d
is a 360x500 matrix.
Basically, each group of 5 columns represent a run. For each run I aggregate
some columns, find which bins each row is in according to column 2, and count
the number of occurrances within each bin. Additionally, I want to find the
median of the aggregated columns values over the rows of a bin and then for each
bin.

brks <- c(0, 25000, seq(125000,1500000,by=100000)) noBins = length(brks)-1
for (r in 1:100) {

```	dsumcost[,r] <- rowSums(d[,((r-1)*5+3):((r-1)*5+5)])
dbins <- findInterval(d[,(r-1)*5+2],brks)
for (b in 1:noBins) {
ids2 <- which(dbins==b)
if (length(ids2)>0) {
medCostBin[r,b] <-
median(dsumcost[ids2,r]/d[ids2,(r-1)*5+2])
noInBin[r,b]<-sum(dbins==b)
} else {
medCostBin[r,b] <- 0
noInBin[r,b] <- 0
}
}
```

}