I want to create group ID by numbering within groups. The important bit here is numbering within groups not global. In the following example, data should be grouped by 'x' and unique ID number created for each unique 'y' value.
df <- data.frame(x=LETTERS[c(1:2, 1, 1:2, 1, 2)], y=LETTERS[c(1, 1, 2, 1, 1, 3, 3)] )
Input
x y
A A
B A
A B
A A
B A
A C
B C
Desired outcome
x y ID
A A 1
B A 1
A B 2
A A 1
B A 1
A C 3
B C 2
I'd favour data.table way of doing it, but all solutions are welcome. I played around with data.table's .GRP and .N and seq_len(.N) to no avail. As it seems a simple and fairly common task, I can't believe no-one has asked it here yet, I sure failed to find it at least.