Search code examples
rdplyrdata-mungingids

Numbering IDs based on group R


I have the dataframe below called df1. I'd like to assign an ID based on as soon as it approaches 'Apple', it changes the ID number to the next ordinal number. When it's assigned 'Apple', I want all the corresponding labels after to be what was assigned for 'Apple'. But as soon as 'Apple' comes up, it goes up a number as with the following labels.

Thanks and much appreciated!

df1 <- data.frame (fruit_name  = c("Apple", "Banana", "Cherry",
                           "Orange", "Blueberry", "Peach",
                           "Apple", "Banana", "Cherry",
                           "Orange", "Blueberry", "Peach",
                           "Apple", "Banana", "Cherry",
                           "Orange", "Blueberry",
                           "Apple", "Cherry"))

into the following

df2 <- data.frame (ID  = c("1", "1", "1", "1", "1", "1", "2", "2",
                           "2", "2", "2", "2", "3", "3", "3", "3", "3",
                           "4", "4"),
                   fruit_name = c("Apple", "Banana", "Cherry",
                                  "Orange", "Blueberry", "Peach",
                                  "Apple", "Banana", "Cherry",
                                  "Orange", "Blueberry", "Peach",
                                  "Apple", "Banana", "Cherry",
                                  "Orange", "Blueberry",
                                  "Apple", "Cherry"))

Solution

  • We can use rowid

    library(data.table)
    df1$ID <- rowid(df1$fruit_name)