Jessica
Jessica

Reputation: 461

t-test for each row between two groups in R

I have two groups: high and low for weight. In addition I have gene expression values for all genes within each group with three varieties:

  gene  Variety_1   Variety_2   Variety_3   Variety_4   Variety_5   Variety_6
Weight  75.01776435 61.33770069 58.94255236 1.378325093 1.257405065 1.238147023
    A1  0.677707    0.546048    1.734473    0.375743    0.337179    0.777027
    A2  7.981616    3.183075    14.36619    6.108213    8.375638    21.607289
    A3  0.867021    2.018648    2.476652    1.634256    1.853648    3.682622
    A4  0           0           0           1.111973    1.970236    5.323712
    A5  1.508041    12.609049   8.554698    2.525282    3.461804    12.971696
    A6  0.371842    1.089097    1.484137    0.94704     0.07742     1.075424
    A7  0           0           8.090433    0           0           0
    A8  0           0           0           0           0           0

I want to perform a t-test for each gene (row) between high (variety 1,2,3) and low (variety 4,5,6) weight varieties.

Upvotes: 0

Views: 253

Answers (2)

Karolis Koncevičius
Karolis Koncevičius

Reputation: 9656

Here is one approach with external library:

library(matrixTests)
row_t_welch(x[,1:3], x[,4:6])

Upvotes: 0

akrun
akrun

Reputation: 887571

We can use apply with MARGIN =1

df1$pval <- c(NA, apply(df1[-1,-1], 1, function(x) t.test(x[1:3], x[4:6])$p.value))

Upvotes: 0

Related Questions