Skip to content
Back to formatted view

Raw Message

Message-ID: <200005031309.JAA17184@cattell.psych.upenn.edu>
Date: 2000-05-03T13:09:07Z
From: Jonathan Baron
Subject: :?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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._