# [R] t-test via matrix operations

Kort, Eric Eric.Kort at vai.org
Wed Aug 14 18:12:10 CEST 2002

```I need to calculate a large number of t statistics, and would like to do so via matrix operations.  So far I have figured out a way to calculate the mean of each row of the matrix:

d <- matrix(runif(100000,1,10), 1000, 10) # some test data
s <- rep(1,ncol(d)) # a sum vector to use for matrix multiplication
means <- (d%*%s)/ncol(d)

This is at least 1 order of magnitude faster than iterating through the rows and using the mean function (<1/100th seconds vs. 13/100th seconds on my computer).

Two questions:
1) Do you see a way to further optimize/simplify/elegantize this calculation of the row means?
2) How do I proceed to calculate the row variances with matrix operations (specifically, how can I use matrix operations to subtract each element of each row from the corresponding row mean)?

Thank you,
Eric Kort
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```