Manuel Vencato
Manuel Vencato

Reputation: 73

R - how to not compare all the conditions with pairwise.t.test

I have this dataframe. I need to compare only and exclusively the condition (the combination of conditions but of course I can merge the two columns) "LS 600" with the condition "SL 600", the condition "LS 750" with the condition "SL 750" and so on.

I'd like to do it automatically using a pairwise t-test in R. I'm able to do it by splitting the dataframe into different smaller dataframes where each one contains a specific condition but it's quite redundant I think.

Of course I tried something like:

pairwise.t.test(df$mean, c(df$condition1,df$condition2),p.adjust.method = "bonferroni", paired = TRUE)

but it doesn't work. And even if it works it does all the comparisons and not just those I want.

N.       ID  condition1    condition2   mean
1         1    LS             600  749.8542
2         1    LS             750  790.3475
3         1    LS             900  869.8517
4         1    LS            1050  760.3375
5         1    SL             600  718.0592
6         1    SL             750  905.0240
7         1    SL             900 1062.8120
8         1    SL            1050  834.5025
9         2    LS             600  869.6608
10        2    LS             750  959.9983
11        2    LS             900  891.0508
12        2    LS            1050  979.2458
13        2    SL             600  860.5100
14        2    SL             750  855.6642
15        2    SL             900 1006.6967
16        2    SL            1050 1008.0583
17        3    LS             600  475.9183
18        3    LS             750  539.8092
19        3    LS             900  704.1117
20        3    LS            1050  721.1008
21        3    SL             600  536.4575
22        3    SL             750  501.9675
23        3    SL             900  586.5667
24        3    SL            1050  651.4267
25        4    LS             600  818.9567
26        4    LS             750  855.7367
27        4    LS             900  828.9883
28        4    LS            1050 1068.5592
29        4    SL             600  828.5730
30        4    SL             750  952.1383
31        4    SL             900 1072.0508
32        4    SL            1050 1019.2450

With redudant I mean this:

splitted_df=split(mean_op_order_df, mean_op_order_df$condition2)

t.test(mean ~ order, data=splitted_df[[1]], paired = TRUE)
t.test(mean ~ order, data=splitted_df[[2]], paired = TRUE)
t.test(mean ~ order, data=splitted_df[[3]], paired = TRUE)
t.test(mean ~ order, data=splitted_df[[4]], paired = TRUE)

and then I need to add manually the Bonferroni correction too.

Upvotes: 0

Views: 422

Answers (1)

Lennyy
Lennyy

Reputation: 6132

If I understand correctly, you want to run several paired t-test, one for each unique observation of variable condition2. The grouping variable should then be condition1.

Assuming your dataframe is called df you could achieve it as follows:

lapply(unique(df$condition2), 
       function(x) pairwise.t.test(df[df$condition2 == x,"mean"],
                                   df[df$condition2 == x,"condition1"],
                                   p.adjust.method = "bonferroni",
                                   paired = T))

[[1]]

    Pairwise comparisons using paired t tests 

data:  df[df$condition2 == x, "mean"] and df[df$condition2 == x, "condition1"] 

   LS  
SL 0.74

P value adjustment method: bonferroni 

[[2]]

    Pairwise comparisons using paired t tests 

data:  df[df$condition2 == x, "mean"] and df[df$condition2 == x, "condition1"] 

   LS  
SL 0.77

P value adjustment method: bonferroni 

[[3]]

    Pairwise comparisons using paired t tests 

data:  df[df$condition2 == x, "mean"] and df[df$condition2 == x, "condition1"] 

   LS  
SL 0.27

P value adjustment method: bonferroni 

[[4]]

    Pairwise comparisons using paired t tests 

data:  df[df$condition2 == x, "mean"] and df[df$condition2 == x, "condition1"] 

   LS  
SL 0.91

P value adjustment method: bonferroni 

Upvotes: 1

Related Questions