Skip to content
Prev 59313 / 398502 Next

how to rewrite this without a loop ?

On Thu, 18 Nov 2004, Stijn Lievens wrote:
You don't say whether `dataset' is the name of a column in `perf.data'. 
Assuming it is, and assuming that 0:29 are all the values of `dataset'

sum(by(perf.data, list(perf.data$dataset),
           function(d)  cor(d[,3],d[,7], method="kendall")))

would work.  If this is faster it will be because you don't call subset() 
twice per iteration, rather than because you are avoiding a loop.  However 
it has other benefits: it doesn't have the variable `i', it doesn't have 
to change the value of `ss', and it doesn't have the range of `dataset' 
hard-coded into it.  These are all clarity optimisations.

 	-thomas