Hello to all R people, 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])} Thanks in advance, Cordialement, -- Nicolas Baurin Doctorant Institut de Chimie Organique et Analytique, UPRES-A 6005 Universit? d'Orl?ans, BP 6759 45067 ORLEANS Cedex 2, France Tel: (33+) 2 38 49 45 77 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Many people suggested apply(). It works for many functions. But if you just need sum(), take a look at rowsum(), it should be faster. Note also, the function rowsum() in Splus2000 is not as fast as in R. Mai Zhou> > Hello to all R people, > > 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])} > > Thanks in advance, > Cordialement, > -- > Nicolas Baurin > > Doctorant > Institut de Chimie Organique et Analytique, UPRES-A 6005 > Universit? d'Orl?ans, BP 6759 > 45067 ORLEANS Cedex 2, France > Tel: (33+) 2 38 49 45 77 > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._