Skip to content
Prev 5498 / 398502 Next

:?alternative to loop

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

--------

apply(m,2,sum)

will do it.  The "2" means "columns".  If you want rows, use "1".
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._