text: user2247: 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) : <text>: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.
  
  user-1: I wrote this snippet a while back to generate all pairwise contrasts:
  
  make_all_contrasts <- function (group, delim="_vs_"){
    
    suppressMessages(require(limma))
    
    #/ ensure that group levels are unique
    group <- sort(unique(as.character(group)))
    
    #/ make all combinations
    cb   <- combn(group, 2, FUN = function(x){paste0(x[1], "-", x[2])})
  
    #/ make contrasts
    contrasts<- limma::makeContrasts(contrasts=cb, levels=group)
    colnames(contrasts) <- gsub("-", delim, colnames(contrasts))
    
    return(contrasts)
  }
  
  
  Assuming three contrasts this will create a limma-like contrast matrix with all possible pairs:
  make_all_contrasts(group=c("groupA", "groupB", "groupC"), delim="_vs_")
  
          Contrasts
  Levels   groupA_vs_groupB groupA_vs_groupC groupB_vs_groupC
    groupA                1                1                0
    groupB               -1                0                1
    groupC                0               -1               -1
  
  
original_text: 2247: <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>
   -1: <p>I wrote this snippet a while back to generate all pairwise contrasts:</p>
  <pre class="lang-r prettyprint-override"><code>
  make_all_contrasts &lt;- function (group, delim=&quot;_vs_&quot;){
    
    suppressMessages(require(limma))
    
    #/ ensure that group levels are unique
    group &lt;- sort(unique(as.character(group)))
    
    #/ make all combinations
    cb   &lt;- combn(group, 2, FUN = function(x){paste0(x[1], &quot;-&quot;, x[2])})
  
    #/ make contrasts
    contrasts&lt;- limma::makeContrasts(contrasts=cb, levels=group)
    colnames(contrasts) &lt;- gsub(&quot;-&quot;, delim, colnames(contrasts))
    
    return(contrasts)
  }
  
  </code></pre>
  <p>Assuming three contrasts this will create a limma-like contrast matrix with all possible pairs:</p>
  <pre><code>make_all_contrasts(group=c(&quot;groupA&quot;, &quot;groupB&quot;, &quot;groupC&quot;), delim=&quot;_vs_&quot;)
  
          Contrasts
  Levels   groupA_vs_groupB groupA_vs_groupC groupB_vs_groupC
    groupA                1                1                0
    groupB               -1                0                1
    groupC                0               -1               -1
  </code></pre>
  