Numbering IDs based on group R

52 Views Asked by At

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"))
2

There are 2 best solutions below

0
On BEST ANSWER

We can use rowid

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

You can use cumsum

df1$ID <- cumsum(df1$fruit_name == 'Apple')