<p>You can use dplyr to extract relevant vectors of values for this comparison. Build out from there using, say, a vector of <code>clus_ft</code> values.</p>
<p>Step-1</p>
<pre class="lang-r prettyprint-override"><code>## untested
run_t_test &lt;- function(df, in_clus_ft) {
  t.test(x = df %&gt;% filter(clus_ft == in_clus_ft &amp; disease == &quot;yes&quot;) %&gt;% pull(median),
         y = df %&gt;% filter(clus_ft == in_clus_ft &amp; disease == &quot;no&quot;) %&gt;% pull(median))
}

run_t_test(df, &quot;A_CD1&quot;)
</code></pre>
<p>Step-2: Now run it on a set of values</p>
<pre><code>sapply(X = c(&quot;A_CD1&quot;,&quot;B_CD1&quot;,&quot;A_IFN&quot;),
FUN = run_t_test,
df = df
)
</code></pre>

CC BY-SA 4.0
2024-04-23T20:46:05.660Z
2024-04-23T20:46:05.660Z
Ram RS
<p>Here is a subset of my df. How can I run pairwise comparisons of median expression between disease status for each clus_ft and use Bonferroni correction?</p>
<p>For example, from this df subset, I would want to perform the following comparisons:</p>
<ol>
<li>A_CD1, no disease vs. A_CD1, with disease</li>
<li>B_CD1, no disease vs. B_CD1, with disease</li>
<li>A_IFN, no disease vs. A_IFN, with disease</li>
</ol>
<p><strong>I do not want to compute pvalues for all possible pairwise comparisons</strong>. For example, the following comparison is irrelevant and should not be computed:</p>
<p><code>A_CD1</code>, no disease vs. <code>A_IFN</code>, with disease</p>
<pre><code>| patient_id | median | disease | clus_ft |
| ---------- | ------ | ------- | ------- |
| 1          | 0.40   | no      | A_CD1   |
| 2          | 0.29   | no      | A_CD1   | 
| 1          | 0.43   | yes     | A_CD1   | 
| 2          | 0.32   | yes     | A_CD1   |
| 1          | 0.77   | no      | B_CD1   | 
| 2          | 0.65   | no      | B_CD1   |
| 1          | 0.01   | yes     | B_CD1   | 
| 2          | 0.06   | yes     | B_CD1   | 
| 1          | 0.86   | no      | A_IFN   | 
| 2          | 0.85   | no      | A_IFN   |
| 1          | 0.02   | yes     | A_IFN   | 
| 2          | 0.24   | yes     | A_IFN   | 
Showing 1 to 12 of 960 entries
</code></pre>

CC BY-SA 4.0
2024-04-28T01:02:12.673Z
Could you consider upvoting and accepting @RamRS answer?
M__
<r><t-test><pairwise>
How can I specify pairwise comparisons of interest rather than computing all possible comparisons with something like Tukey?
# How can I specify pairwise comparisons of interest rather than computing all possible comparisons with something like Tukey?

(Asked by: username_0 on 2024-04-23)

Here is a subset of my df. How can I run pairwise comparisons of median expression between disease status for each clus_ft and use Bonferroni correction?
For example, from this df subset, I would want to perform the following comparisons:

A_CD1, no disease vs. A_CD1, with disease
B_CD1, no disease vs. B_CD1, with disease
A_IFN, no disease vs. A_IFN, with disease

I do not want to compute pvalues for all possible pairwise comparisons. For example, the following comparison is irrelevant and should not be computed:
A_CD1, no disease vs. A_IFN, with disease
| patient_id | median | disease | clus_ft |
| ---------- | ------ | ------- | ------- |
| 1          | 0.40   | no      | A_CD1   |
| 2          | 0.29   | no      | A_CD1   | 
| 1          | 0.43   | yes     | A_CD1   | 
| 2          | 0.32   | yes     | A_CD1   |
| 1          | 0.77   | no      | B_CD1   | 
| 2          | 0.65   | no      | B_CD1   |
| 1          | 0.01   | yes     | B_CD1   | 
| 2          | 0.06   | yes     | B_CD1   | 
| 1          | 0.86   | no      | A_IFN   | 
| 2          | 0.85   | no      | A_IFN   |
| 1          | 0.02   | yes     | A_IFN   | 
| 2          | 0.24   | yes     | A_IFN   | 
Showing 1 to 12 of 960 entries

**Tags:** r, t-test, pairwise
> **Comments:**
> **username_1**: Could you consider upvoting and accepting @RamRS answer?  

---

## 1 answers

### Answer by username_2 on 2024-04-23. Score: 2

You can use dplyr to extract relevant vectors of values for this comparison. Build out from there using, say, a vector of clus_ft values.
Step-1
## untested
run_t_test <- function(df, in_clus_ft) {
  t.test(x = df %>% filter(clus_ft == in_clus_ft & disease == "yes") %>% pull(median),
         y = df %>% filter(clus_ft == in_clus_ft & disease == "no") %>% pull(median))
}

run_t_test(df, "A_CD1")

Step-2: Now run it on a set of values
sapply(X = c("A_CD1","B_CD1","A_IFN"),
FUN = run_t_test,
df = df
)


---

