[R]:?alternative to loop

Jonathan Baron baron at cattell.psych.upenn.edu
Wed May 3 15:09:07 CEST 2000


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list