[R] Advice on Speed Improvement

Werner Wernersen pensterfuzzer at yahoo.de
Sun Apr 24 19:05:27 CEST 2005


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
		}
	}
}

Thank you for your consideration!

Best,
   Werner




More information about the R-help mailing list