Making a new column by subtracting values based on a key in R?
<p>I have a data table like this</p>

<pre><code>ID      DAYS    FREQUENCY
"ads"   20      3
"jwa"   45      2
"mno"   4       1
"ads"   13      3
"jwa"   60      2
"ads"   18      3
</code></pre>

<p>I want to add a column that subtracts the days based on the id and subtract the closest days together.
My new table would like like this:</p>

<pre><code>ID      DAYS    FREQUENCY    DAYS DIFF
"ads"   20      3            2 (because 20-18) 
"jwa"   45      2            NA (because no value greater than 45 for that id)
"mno"   4       1            NA
"ads"   13      3            NA
"jwa"   60      2            15
"ads"   18      3            5
</code></pre>

<p>Bonus: Is there a way to use the merge function?</p>