[R] compute variance of every column in a matrix without a loop

Thomas Lumley tlumley at u.washington.edu
Sun Mar 17 23:50:00 CET 2002


On Sat, 16 Mar 2002, Francisco J Molina wrote:

> Is it possible to compute the variance of every column in a matrix
> without a loop?

No, but you can make it look as if there's no loop.

You can hide the for() loop inside a function with
  apply(m, 2, var)
if the reason you want to avoid a loop is to make your code look simpler.

In 1.5.0 you will be able to do
   colMeans(m*m)-colMeans(m)^2
if the reason is to have the loop take place in compiled code and be
faster.


	-thomas


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