title: R lm on each factor level
body: <p>I'm hoping to find some guidance on both the how-to-code level and on the how-to-solve level in R.  I have a simple data frame as follows:</p>
  
  <pre><code>str(pf)
     'data.frame':  1536 obs of 4 variables:
     $ dt : POSIXct 
     $ pfRet : num 
     $ Src : Factor w/6 Levels "ALPA" , "OMEGA", "GAMMA" , ..
     $ ret : num
  </code></pre>
  
  <p>I simply want to see lm(pfRet ~ ret ) for each of the 6 different factors and compare them.  I would love to be able to get to an output showing sorted by the p value for the lm for each factor level.  I am trying to ask the question, for each factor ( ALPHA, OMEGA, GAMA, et al), which one best explains the relationship between pfRet and ret?</p>
  
  <p>From a programming perspective there's probably a way to get a vector of the 6 factor levels and then apply lm over the frame.  But I couldn't get it to work right.  I tried something along the lines of:</p>
  
  <pre><code>GenLm = function(x) { 
    y=pf[which[(pf$Src==x,)]
    return(lm(y$pfRet ~ y$ret )) }
  
  testList = data.frame( Src = as.character(unique( pf$Src ) ) )
  
  testList$lm = apply(  ... ?? )
  </code></pre>
  
  <p>I tried various combinations of apply, ddply, etc.  I just couldn't get it into an easy format where an output could be generated showing the fits for each variable.</p>
  
  <p>Thanks, Josh
  From a higher level perspective, is this the right way to go about it?  This is something surely that R users deal with all of the time.</p>