doing t.test for columns for each row in data set

leila aghili picture leila aghili · Jan 23, 2015 · Viewed 10.6k times · Source

I have a set of data x which consists of 12 columns and 167 rows. The first column is compound Id for each row. I want to run a t.test for 3 column as one group and the other 3 groups as the second group, separately for each row. My code is as below but it does not work.

for (i in 1:nrow(x)) {
 function(i)c(compound=i,
    t.test(x[2:4],x[8:10],
      x[x$compound==i, ],
      alternative='two.sided',conf.level=0.95)
    )
}
print(c(compound=i,t.test(x[2:4],x[8:10],x[x$compound==i,],
    alternative='two.sided',conf.level=0.95)))

My intention was doing a t.test for each metabolite (compound) between AC groups and SC groups, these are two group of cells.

compound    AC-1     AC-2     AC-3     AM-1      AM-2      AM-3      SC-1     SC-2     SC-3     SM-1      SM-2      SM-3
alanine     27612820 22338050 15359640 19741350  18726880  18510800  10914980 12071660 16036180 16890860  16066960  16364300
arginine    7067206  7172234  5933320  136272600 131596800 134717600 6102838  7186256  6770344  140127100 155341300 151748000
asparagine  3151398  2141378  1240904  11522180  8907711   9842342   1677299  2265826  2942991  11690360  12552660  12102620                        

Answer

Bryan picture Bryan · Jan 24, 2015

The t.test is used to compare two data sets. Collecting two data sets each from three different columns of a matrix can be done like this:

data_a = c(x[,2:4])
data_b = c(x[,4:8])

These two data sets can be evaluated using t.test at this point:

t.test(data_a, data_b)

Collecting the data from three columns each for two different compounds for a given row (amino acid) we modify and add a loop:

x <- matrix(rnorm(24, mean=0, sd=1), 4, ncol=6)
x
           [,1]       [,2]        [,3]      [,4]       [,5]       [,6]
[1,] -0.4810307  0.3996071  0.90663635 0.7487048  0.5787846  2.0231681
[2,] -2.0454921 -0.1225105 -1.04447522 0.9325333 -1.7782776  0.6856150
[3,] -0.3099937  1.2079548 -0.03835271 0.2751349  1.0111554 -0.4862846
[4,] -0.2834953  0.1930481 -0.57968344 0.1204925 -0.5015843  0.3690397

for(i in 1:nrow(x)){
data_a = c(x[i, 1:3])
data_b = c(x[i, 4:6])
print(t.test(data_a, data_b))
}