<p>I am trying to generate contrast matrix for different conditions using <strong>for</strong> loop.</p>
<pre><code>cluster_len &lt;- 3
req_contrasts &lt;- c()

for (m in 1:cluster_len) {
  for (n in 1:cluster_len) {
    if (n &gt; m) {
      req_contrasts &lt;- c(req_contrasts, (paste0(paste0(&quot;Group&quot;, n), &quot;-&quot;, paste0(&quot;Group&quot;, m))))
  
    }
  }
}
</code></pre>
<p>req_contrasts returns
<code>&quot;Group2-Group1&quot; &quot;Group3-Group1&quot; &quot;Group3-Group2&quot;</code></p>
<p>The contrast that needs to be passed is
<code>&quot;Group2-Group1&quot;,&quot;Group3-Group1&quot;,&quot;Group3-Group2&quot;</code></p>
<p>I have tried the following code to combine each string and form a single vector</p>
<pre><code># To separate each condition by a double quote and comma
temp_1 &lt;- paste0(req_contrasts, collapse='&quot;,&quot;', sep=&quot;&quot;)
temp_1
[1] &quot;Group2-Group1\&quot;,\&quot;Group3-Group1\&quot;,\&quot;Group3-Group2&quot;
# To remove the escape character
temp_2 &lt;- gsub(&quot;\\&quot;, &quot;&quot;, temp_1, fixed = TRUE)
temp_2
[1] &quot;Group2-Group1\&quot;,\&quot;Group3-Group1\&quot;,\&quot;Group3-Group2&quot;
</code></pre>
<p>This does not work</p>
<pre><code>contrast.matrix &lt;- makeContrasts(temp_2, levels=design_mat)
Error in parse(text = ej) : &lt;text&gt;:1:14: unexpected string constant
1: Group2-Group1&quot;,&quot;
^
</code></pre>
<p>Limma throws the following error in presence of escape character. I am assuming it is because it considers that the string is yet to end.</p>
<p>Following input works</p>
<pre><code>contrast.matrix_2 &lt;- makeContrasts(&quot;Group2-Group1&quot;, &quot;Group3-Group1&quot;, &quot;Group3-Group2&quot;,                 levels=design_mat)
</code></pre>
<p>In my analysis the number of clusters to be analysed is 16. Hence, the contrast conditions cannot be generated manually.</p>

<limma><loop>
Generating contrast matrix for limma in loop
# Generating contrast matrix for limma in loop

(Asked by: username_0 on 2022-02-13)

I am trying to generate contrast matrix for different conditions using for loop.
cluster_len <- 3
req_contrasts <- c()

for (m in 1:cluster_len) {
  for (n in 1:cluster_len) {
    if (n > m) {
      req_contrasts <- c(req_contrasts, (paste0(paste0("Group", n), "-", paste0("Group", m))))
  
    }
  }
}

req_contrasts returns
"Group2-Group1" "Group3-Group1" "Group3-Group2"
The contrast that needs to be passed is
"Group2-Group1","Group3-Group1","Group3-Group2"
I have tried the following code to combine each string and form a single vector
# To separate each condition by a double quote and comma
temp_1 <- paste0(req_contrasts, collapse='","', sep="")
temp_1
[1] "Group2-Group1\",\"Group3-Group1\",\"Group3-Group2"
# To remove the escape character
temp_2 <- gsub("\\", "", temp_1, fixed = TRUE)
temp_2
[1] "Group2-Group1\",\"Group3-Group1\",\"Group3-Group2"

This does not work
contrast.matrix <- makeContrasts(temp_2, levels=design_mat)
Error in parse(text = ej) : :1:14: unexpected string constant
1: Group2-Group1","
^

Limma throws the following error in presence of escape character. I am assuming it is because it considers that the string is yet to end.
Following input works
contrast.matrix_2 <- makeContrasts("Group2-Group1", "Group3-Group1", "Group3-Group2",                 levels=design_mat)

In my analysis the number of clusters to be analysed is 16. Hence, the contrast conditions cannot be generated manually.

**Tags:** limma, loop

---

## 0 answers

