title: Correlation of subsets of dataframe using aggregate
body: <p>I have a data frame made by row binding many data frames, each identified with a unique key. I wish to calculate the correlation coefficients for columns in each subset (using the unique key) of the big data frame. For example, using the mtcars data I might want to calculate the correlation between columns <code>hp</code> and <code>wt</code> for each unique value in column <code>cyl</code>. I could do it in a loop</p>
  
  <pre><code>data("mtcars")
  for(i in c(4,6,8)){
  temp = subset(mtcars,mtcars$cyl==i)
  cor(temp$hp,temp$wt)
  }
  </code></pre>
  
  <p>I think aggregate would be better, but this code doesn't work:</p>
  
  <pre><code>data("mtcars")
  aggregate(mtcars,by=mycars$cyl,cor)
  </code></pre>