[R] Row-wise two sample T-test on subsets of a matrix

Tim Hesterberg timh at insightful.com
Mon Mar 12 23:44:09 CET 2007


This is the kind of thing that rowMeans was made for.
For the numerator of the t statistic:

x1 <- temp.matrix[,1:11]
x2 <- temp.matrix[,12:22]
numerator <- rowMeans(x1) - rowMeans(x2)

For the denominator, if you're using S+ you can use rowVars;
in R you can program a simple version quickly, e.g.

rowVars <- function(x){
  means <- rowMeans(x)
  rowSums((x - means)^2) / (ncol(x)-1)
}

Tim Hesterberg

>Hello all,
>
>I am trying to run a two sample t-test on a matrix which is a
>196002*22 matrix. I want to run the t-test, row-wise, with the
>first 11 columns being a part of the first group and columns
>12-22 being a part of the second group. 
>
>I tried running something like (temp.matrix being my 196002*22
>matrix)
>
>t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE)
>
>or somthing like
>
>as.numeric(t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE)[[1]])
>so as to only capture the t-value alone and 
>
>and I get a result for the whole matrix instead of a row-wise
>result. 
>
>I want to avoid using a "for" loop to increment the number of
>rows as it would take a huge amount of time.
>
>
>Any suggestions would be really appreciated.
>
>thanks
>nameeta

========================================================
| Tim Hesterberg       Senior Research Scientist       |
| timh at insightful.com  Insightful Corp.                |
| (206)802-2319        1700 Westlake Ave. N, Suite 500 |
| (206)283-8691 (fax)  Seattle, WA 98109-3044, U.S.A.  |
|                      www.insightful.com/Hesterberg   |
========================================================
Advanced Programming in S-PLUS
		30 Apr-1 May UK, 7-8 May CH, 9-10 May FR
		May 17-18 Philadelphia, Oct 8-9 San Francisco
		26-7 Sep CH, 1-2 Oct UK		
		http://www.insightful.com/services/training.asp



More information about the R-help mailing list