I have a df like this:
A B
0 0
0 0
0 0
0 1
0 1
0 2
0 3
0 3
1 0
1 0
1 1
1 1
2 0
2 1
2 2
I need a new column C with an iterator which counts the number of occurences of value in column B.
This is what exactly I need:
A B C
0 0 1
0 0 2
0 0 3
0 1 1
0 1 2
0 2 1
0 3 1
0 3 2
1 0 1
1 0 2
1 1 1
1 1 2
2 0 1
2 1 1
2 2 1
First 3 rows of C are 1-2-3 beacause in B we have 3 rows with value 0, then 2 rows of C with 1-2 beacause we have two rows with value 1 in B, etc...
I tried with something like this:
DF$C <- ifelse(DF$B == 0 , 1:length(DF),1:length(DF))
But actually it doesn't work with more value than 0, and can't control it quite well. I need some for loop that checks col B and create col C iterating it.
Hope the question is clear. Thank you in advance.