I am trying to create an index (i.e. a column thats up from 0-n) for different group levels of a factor variable, all within one column. Specifically, here is a minimal working example, for data frame D:
ID Index
1 1
1 2
2 1
2 2
3 1
3 2
3 3
I currently have column 1, and would like to get column 2. I have struggled way more than I would like with this problem. I have tried several things, but the following seems like it should have worked:
dlply(D, .(ID), function(D){D$index = seq.int(nrow(D$ID))})