I am looking for a smart way to index subcategories within a dataframe.
I've created a very simple reproducible example below. How would you code the following step to go from input to output (ie how can we code the creation of color_id variable)?
Thank you very much in advance for your view on this!
input <- data.frame(label = c("red", "red", "blue", "green", "green", "green", "orange"), count = c(2, 2, 1, 3, 3 ,3, 1))
output <- data.frame(label = c("red", "red", "blue", "green", "green", "green", "orange"), count = c(2, 2, 1, 3, 3 ,3, 1), color_id = c(1, 2, 1, 1, 2, 3, 1))
Best regards