[R]:?alternative to loop
Uwe Ligges
ligges at statistik.uni-dortmund.de
Wed May 3 13:34:12 CEST 2000
nicolas baurin wrote:
> for the application of a function to each column of a matrix (ex:
> function sum()), i'd like to know if there is a simpler (and faster)
> alternative to a loop like :
>
> for (i in 1: ncol(m)){sum(m[i])}
This will not work in any way, I suppose.
If you want to simplify something like
s <- NULL
for (i in 1: ncol(m)){ s[i] <- sum(m[,i])}
you should try
s <- apply(m, 2, sum)
Have a look in documentation to apply(.) !
Regards,
Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list