3

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))})
Tung
  • 26,371
  • 7
  • 91
  • 115
user3859248
  • 97
  • 2
  • 11
  • 1
    Try to `group_by` and then `data.table::rleid` to create the new column. You can use `data.table::rleid` inside `mutate`. – Andrew Apr 13 '19 at 19:49
  • 5
    `library(dplyr); df %>% group_by(ID) %>% mutate(Index = row_number())` – Tung Apr 13 '19 at 19:53
  • 1
    @Tung Thank you, this solution worked! If you add a post below I will mark it as the correct one. Andrew I did not try your solution, but thank you. – user3859248 Apr 13 '19 at 20:00
  • 1
    No worries, as long as your problem is solved! – Andrew Apr 13 '19 at 20:15
  • 1
    @user3859248: no worry. Glad it solved your problem. You can post the answer yourself to help future readers if you want to – Tung Apr 13 '19 at 20:44

1 Answers1

4

The answer Tung provided in the comments of the original question worked for me. Providing it below for completeness:

library(dplyr)
df %>% 
  group_by(ID) %>% 
  mutate(Index = row_number())
user3859248
  • 97
  • 2
  • 11