Combining specific row data in R
<p>Hi I have a dataset where one row presents a item in an order. One order can have more items and therefore more rows in a dataset.  </p>

<p>data looks like:</p>

<pre><code>    code        nr. Of items
1   252222016   5
3   252812016   1
5   252812016   1
6   253012016   1
12  253042016   20
13  253042016   20
15  253052016   1
16  253072016   3
18  253082016   4
</code></pre>

<p>I would like a result where I would summ the "nr. Of items" where there is a same "code" and write the resoult in the row. if one order has more items I would like to have a resoult written in the last row of an order and previous rows would be NAs.</p>

<p>how I want the final solution to look like:</p>

<pre><code>        code    nr. Of items    result
1   252222016   5                 5
3   252812016   1                na
5   252812016   1                 2
6   253012016   1                 1
12  253042016   20               na
13  253042016   20               40
15  253052016   1                 1
16  253072016   3                 3
18  253082016   4                 4
</code></pre>

<p>I would appreciate the help!</p>