[R] how to rewrite this without a loop ?

Stijn Lievens stijn.lievens at ugent.be
Thu Nov 18 15:44:21 CET 2004


Dear Rexperts,

First of all let me say that R is a wonderful and useful piece of 
software.

The only thing is that sometimes it takes me a long time to find out how 
something can be done, especially when aiming to write compact (and 
efficient) code.

For instance, I have the following function (very rudimentary) which 
takes a (very specific) data frame as input and for certain subsets
calculates the rank correlation between two corresponding columns.
The aim is to add all the rank correlations.

<code>
add.fun <- function(perf.data) {
    ss <- 0
    for (i in 0:29) {
    	ss <- ss + cor(subset(perf.data, dataset == i)[3], 
subset(perf.data, dataset == i)[7], method = "kendall")
    }
    ss	
}
</code>

As one can see this function uses a for-loop.  Now chapter 9 of 'An 
introduction to R' tells us that we should avoid for-loops as much as 
possible.

Is there an obvious way to avoid this for-loop is this case ?

I would like to see something in the lines of

(maple style)

<code>
add( seq(FUN(i), i = 0..29) )
</code>

Greetings

Stijn.


-- 
==========================================================================
Dept. of Applied Mathematics and Computer Science, University of Ghent
Krijgslaan 281 - S9, B - 9000 Ghent, Belgium
Phone: +32-9-264.48.91, Fax: +32-9-264.49.95
E-mail: Stijn.Lievens at ugent.be, URL: http://allserv.ugent.be/~slievens/




More information about the R-help mailing list