title: How to loop through all possible factor level comparisons in R
body: <p>Consider the following dataframe:</p>
  
  <pre><code>type = c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D')
  val1 = c(.35, .36, .35, .22, .27, .25, .88, .9, .87, .35, .35, .36)
  val2 = c(.35, .35, .37, .40, .42, .46, .9, .91, .82, .36, .36, .36)
  
  df = data.frame (type, val1, val2)
  </code></pre>
  
  <p>I have four categories (called types; A, B, C, and D). The three observations of each type can be averaged to create a type multivariate mean (composed of the means of val1 and val2). I would like to compare all possible combinations of types (AB, AC, AD, BC, BD, CD) using Hotelling's test to determine which type means (if any) are the same. I could hard code this as:</p>
  
  <pre><code>a = filter (df, type == "A") [,2:3]
  b = filter (df, type == "B") [,2:3]
  c = filter (df, type == "C") [,2:3]
  d = filter (df, type == "D") [,2:3]
  </code></pre>
  
  <p>And then run Hotelling's T2 test for each specified pair of types:</p>
  
  <pre><code>library('Hotelling')
  hotelling.test(a, b, shrinkage=FALSE)
  hotelling.test(b, c, shrinkage=FALSE)
  hotelling.test(a, c, shrinkage=FALSE)
  
  #And so on
  </code></pre>
  
  <p>This is obviously very inefficient and impractical, given that my actual dataset has 55 different types. I know the answer lies in for loops, but I'm having a hard time figuring out how to tell hotelling.test to compare the val1/val2 multivariate means for all possible type combinations. I'm very new to creating for loops and was hoping someone could point me in the right direction. </p>
  
  <p>After comparing all of the types, I'd then ideally be able to get an output that shows the type pairs for which the Hotelling test p-value was >0.05, meaning that those two types are likely duplicates. In the example dataframe, types A and D return a p-value >0.05, while the other comparisons have p&lt;0.05.</p>