title: Rank correlation matrix in R
body: <p>How do I produce a rank correlation matrix in an elegant way in R given a data frame with many columns? I couldn't find a built-in function, so I tried</p>
  
  <pre><code>&gt; test=data.frame(x=c(1,2,3,4,5), y=c(5,4,3,2,1))
  &gt; cor(rank(test))
  </code></pre>
  
  <p>(only 2 columns for simplicity, real data has 5 columns) which gave</p>
  
  <pre><code>&gt; Error in cor(rank(test)) : supply both 'x' and 'y' or a matrix-like 'x'
  </code></pre>
  
  <p>I figured that this was because <code>rank</code> takes a single vector. So then I tried </p>
  
  <pre><code>&gt; cor(lapply(test,rank))
  </code></pre>
  
  <p>to get rank applied to <em>each column</em> in the data frame, treating the data frame as a list, which gave the error </p>
  
  <pre><code>&gt; supply both 'x' and 'y' or a matrix-like 'x'
  </code></pre>
  
  <p>and I finally ended up getting something working with</p>
  
  <pre><code>&gt; cor(data.frame(lapply(test,rank)))
     x  y
  x  1 -1
  y -1  1
  </code></pre>
  
  <p>However this seems pretty verbose and ugly. I'm thinking there must be a better way -- if so what?</p>