title: R - correlation between column subsets - reference current row
body: <p>I have a set of data such as;</p>
  
  <pre><code>       name      Exp1Res1   Exp1Res2   Exp1Res3   ExpRes1   Exp2Res2   Exp3Res3
  
  [1]     ID1         5          7            9          7          9       2 
  
  [2]     ID2         6          4            2          9          5       1
  
  [3]     ID3         4          9            9          9          11      2
  </code></pre>
  
  <p>I need to determine the correlation between experiment 1 and 2 for each row. As there are actually 37 columns and 100,000 rows in my dataset (FullSet), my original solution of looping through is far too slow (refer below), so I wanted to optimize.</p>
  
  <p>My original solution was; </p>
  
  <pre><code>df &lt;- data.frame(matrix(ncol = 5, nrow = dim(FullSet)[1]))
  names(df)&lt;-c("ID","pearson","spearman")
  for (i in  seq(1, dim(FullSet)[1]))  
  {
      pears=cor(as.numeric(t(FullSet[i,2:19])),as.numeric(t(FullSet[i,20:37])), method="pearson")
      spear=cor(as.numeric(t(FullSet[i,2:19])),as.numeric(t(FullSet[i,20:37])), method="pearson")
      df[i,]&lt;-c(FullSet[i,1],pears,spear)
  }
  </code></pre>
  
  <p>I feel something like this should work;</p>
  
  <pre><code>FullSet$pearson&lt;-cor(as.numeric(t(FullSet[,2:19])),as.numeric(t(FullSet[,20:37])), method="pearson")
  </code></pre>
  
  <p>but I don't know if/how to reference just the current row in the transpose -  </p>
  
  <pre><code> t(FullSet[,2:19]) - which should read something like t(FullSet[&lt;currow&gt;,2:19]). 
  </code></pre>
  
  <p>Help would be appreciated - I don't know if my approach is even correct.</p>
  
  <p>Output should look like (Results are not correct - for example only)</p>
  
  <pre><code>       name      Pearson     Spearman
  
  [1]     ID1         0.8          .75 
  
  [2]     ID2         0.9          .8
  
  [3]     ID3         0.85         .7
  </code></pre>