title: Correlations for pairs of combinations
body: <p>I have 22 variables, and I'd like to get the correlation scores, not as a matrix of correlation, but in a data frame, by pairs...</p>
  
  <p>I mean... Not like this</p>
  
  <pre><code>    v1  v2  v3  v4
  v1  1   x   x   x
  v2  x   1   x   x
  v3  x   x   1   x
  v4  x   x   x   1
  </code></pre>
  
  <p>but like this:</p>
  
  <pre><code>var1  var2 cor
  v1    v2   x
  v1    v3   x
  v1    v4   x
  v2    v3   x
  v2    v4   x
  v3    v4   x
  </code></pre>
  
  <p>I'm new to R and I have been researching a lot, and I end up with a code that, sincerely, Is not efficient at all... My code creates a huge data frame with all the possible combinations for 22 variables (which is 4194304 combinatios... <em>a lot!!!</em> ) ... And then the code assigns  the correlations just for the first 211 rows, which are the combinations with only 2 variables... Then I exclude everything I'm not interested in. Well... I get what I need. But I'm sure this is a very dumb way to do this and I'd like to learn a better way...
  Any tips?</p>
  
  <p>My code:</p>
  
  <pre><code>#Getting the variable names from the data frame
  av_variables&lt;-variable.names(data.1)
  
  #Creating a huge data frame for all possible combinations
  corr_combinations &lt;- as.data.frame(matrix(1,0,length(av_variables)))
  for (i in 1:length(av_variables)){
    corr_combinations.i &lt;- t(combn(av_variables,i))
    corr_combinations.new &lt;- as.data.frame(matrix(1,length(corr_combinations.i[,1]),length(av_variables)))
    corr_combinations.new[,1:i] &lt;- corr_combinations.i
    corr_combinations &lt;- rbind(corr_combinations,corr_combinations.new)
  
  #How many combinations for 0:2 variables?
  comb_par_var&lt;-choose(20, k=0:2)
  ##211
  
  #A new column to recieve the values
  corr_combinations$cor &lt;- 0
  
  
    #Getting the correlations and assigning to the empty column
   for (i in (length(av_variables)+1):(length(av_variables)+ sum(comb_par_var) +1)){
    print(i/length(corr_combinations[,1]))
    corr_combinations$cor[i] &lt;- max(as.dist(abs(cor(data.1[,as.character(corr_combinations[i,which(corr_combinations[i,]!=0&amp;corr_combinations[i,]!=1)])]))))
    # combinations$cor[i] &lt;- max(as.dist(abs(cor(data.0[,as.character(combinations[i,combinations[i,]!=0&amp;combinations[i,]!=1])]))))
    }
  
  #Keeping only the rows with the combinations of 2 variables
  corr_combinations[1:(length(av_variables)+ sum(comb_par_var) +2),21]
  corr_combinations&lt;-corr_combinations[1:212,]
  corr_combinations&lt;-corr_combinations[21:210,]
  
  #Keeping only the columns var1, var2 and cor
  corr_combinations&lt;-corr_combinations[,c(1,2,21)]
  
  #Ordering to keep only the pairs with correlation &gt;0.95, 
  #which was my purpose the whole time
  corr_combinations &lt;- corr_combinations[order(corr_combinations$cor),]
  corr_combinations&lt;-corr_combinations[corr_combinations$cor &gt;0.95, ] 
  }
  </code></pre>