Group rows based on a condition and add sum column of unique values : Here an example of my data.frame:

    df = read.table(text='colA   colB   colC
    10  11   7
    10  34   7
    10  89   7
    10  21   7
    2   23   5
    2   21   5
    2   56   5
    22  14   3
    22  19   3
    22  90   3
    1   45   0
    1   23   0
    9   8    0
    9   11   0
    9   21   0', header = TRUE)

I need to group the rows by `colA` and `colC` and add a new column which states the sum of unique values based on `colB`.

My conditional statement could be:

For each `colC` diverse than zero group each `colA` value and its preceding one (i.e. 10 and 9, 2 and 1) and add a new column based on the sum of `colB` unique values (for each group).

My expected output should be:

    colA   colB   colC  newcolD
       10  11   7      5
       10  34   7      5
       10  89   7      5
       10  21   7      5
       9   8    0      5
       9   11   0      5
       9   21   0      5
       2   23   5      4
       2   21   5      4
       2   56   5      4
       1   45   0      4
       1   23   0      4
       22  14   3      3
       22  19   3      3
       22  90   3      3

To note that in `df` the `colB` duplicated values are: 11 and 21 for group 10 and 9, and 23 for group 2 and 1.

Thanks for any help given.


  
