title: Transform rows to columns in R for statistical correlation analysis
body: <p>I am attempting to transform columns to rows in R, without the use of reshape (can't install the package). The data I have received includes attributes and their corresponding metrics. I would like to calculate the statistical correlation between all of these attributes--16000 in total with 8 million records. Not all records have the same number of attributes.</p>
  
  <p>To do this, I believe I will have to convert columns to rows so that I can eventually use the cor function e.g. cor(x[,1], x[,2:16000]). This may be wholly unnecessary if there is some way to use the cor function by attribute i.e. correlation between attribute 1 and 2, attribute 1 and 3, attribute 1 ... N. Any help would be much appreciated.</p>
  
  <pre><code> ID          Attribute  Metric1 
   [1,]  1         1 -1.6363007
   [2,]  2         1  1.1483294
   [3,]  3         1  2.1682566
   [4,]  4         1 -1.1823649
   [5,]  5         1 -1.3631378
   [6,]  1         2 -1.1715544
   [7,]  2         2  1.5164278
   [8,]  3         2 -1.0110274
   [9,]  4         2 -0.9421652
  [10,]  5         2 -0.2105443
  [11,]  6         2 -0.4143548
  [12,]  7         2 -1.6170975
  [13,]  8         2  1.2402303
  [14,]  9         2  0.4460047
  [15,]  7         3  0.1060407
  [16,]  8         3  0.9796893
  [17,]  9         3  0.9254911
  [18,] 10         3 -1.5728600
  [19,] 11         3 -0.8082675
  [20,] 12         3 -1.8643084
  </code></pre>
  
  <p>Transformation:</p>
  
  <pre><code>ID  attribute1  attribute2  attribute3
  1   -1.6363007  -1.1715544  na
  2   1.1483294   1.5164278   na
  3   2.1682566   -1.0110274  na
  4   -1.1823649  -0.9421652  na
  5   -1.3631378  -0.2105443  na
  6   na          -0.4143548  na
  7   na          -1.6170975  0.1060407
  8   na           1.2402303  0.9796893
  9   na           0.4460047  0.9254911
  10  na           na         -1.57286
  11  na           na         -0.8082675
  12  na           na         -1.8643084
  
  
  test &lt;- cbind(c(rep(1,5),rep(2,9),rep(3,6)), replicate(1,rnorm(20)))
  test &lt;- cbind(c(1:5,1:9,7:12),test)
  </code></pre>
  
  <p>@Aaron </p>
  
  <pre><code>q &lt;- matrix(nrow=20,ncol=3)
  colnames(q) &lt;- c("x","y","z")
  q[,3] &lt;- replicate(1, rnorm(20))
  q[,2] &lt;- c(101,102,103,104,105,106, 107, 108, 101,103,107,109, 104,110,102,103,106,109,108,112)
  q[15:20,1] &lt;- 10000003
  q[9:14,1] &lt;- 10000002
  q[1:8,1] &lt;- 10000001
  q &lt;- data.frame(q)
  q$x &lt;- factor(q$x)
  q$y &lt;- factor(q$y)
  q$z &lt;- factor(q$z)
  
  with(q, {
    out &lt;- matrix(nrow=nlevels(x), ncol=nlevels(y),
                  dimnames=list(levels(x), levels(y)))
    out[cbind(x, y)] &lt;- z
    out
  })
  </code></pre>