title: Easiest way to subtract associated with one factor level from values associated with all other factor levels
body: <p>I've got a dataframe containing rates for 'live' treatments and rates for 'killed' treatments. I'd like to subtract the killed treatments from the live ones:</p>
  
  <pre><code>df &lt;- data.frame(id1=gl(2, 3, labels=c("a", "b")),
                   id2=rep(gl(3, 1, labels=c("live1", "live2", "killed")), 2), 
                   y=c(10, 10, 1, 12, 12, 2),
                   otherFactor = gl(3, 2))
  </code></pre>
  
  <p>I'd like to subtract the values of <code>y</code> for which <code>id2=="killed"</code> from all the other values of <code>y</code>, separated by the levels of id1, while preserving <code>otherFactor</code>. I would end up with </p>
  
  <pre><code>id1    id2   y otherFactor
    a  live1   9           1
    a  live2   9           1
    b  live1  10           2
    b  live2  10           3
  </code></pre>
  
  <p>This almost works:</p>
  
  <pre><code>df_minusKill &lt;- ddply(df, .(id1), function(x) x$y[x$id2!="killed"] - x$y[x$id2=="killed"])
  names(df_minusKill) &lt;- c("id1", "live1", "live2")
  df_minusKill_melt &lt;- melt(df_minusKill, measure.var=c("live1", "live2"))
  </code></pre>
  
  <p>except that you lose the values of otherFactor. Maybe I could use <code>merge</code> to put the values of <code>otherFactor</code> back in, but in reality I have about a dozen "otherFactor" columns, so it would be less cumbersome to just keep them in there automatically.</p>