 <p><code>tapply</code> and <code>transform</code>?</p> <pre><code>&gt; transform(df, volumen=unlist(tapply(cumVol, farm, function(x) c(0, diff(x))))) period farm cumVol other volumen A1 1 A 1 1 0 A2 2 A 5 2 4 A3 3 A 15 3 10 A4 4 A 31 4 16 B1 1 B 10 5 0 B2 2 B 12 6 2 B3 3 B 16 7 4 B4 4 B 24 8 8 </code></pre> <p><code>ave</code> is a better option, see @ thelatemail's comment</p> <pre><code>with(df, ave(cumVol,farm,FUN=function(x) c(0,diff(x))) ) </code></pre>