[R] Applying t-test to matrix without using a loop

Cedric Laczny cedric.laczny at gmx.de
Tue Aug 24 21:09:39 CEST 2010


Hi,

the actual thread on "multiple assignments ?" made up an interesting point for 
me.
If I have a matrix with g rows and x + y columns where columns 1 - x contain 
values of group1 and columns x+1 to y contain the values of group2.
Now I want to compute a vector of length g that holds the p-value (e.g. via a 
t-test) calculated for the appropriate row in the matrix.

At the moment I am doing something like this:
# code might be wrong. Should just give the idea.
p = rep(-1, g)
for( i in 1: g)
p[i] = t.test(values[i, c(1:x)], values[i, c(x +1: x+y)]

I am now wondering if there is a faster (maybe also more elegant) way to do 
this, e.g. using something like lapply or such?

Best,

Cedric



More information about the R-help mailing list