Hi guys, I have a question: I got codes from supermarket products and have to redirect them to their sectors. All the products have 5-digit. The problem is that products that start with the number 0, become a 4-digit product, so the 0 goes away and the product goes to clothes. FJCC helped me another time but now I don't get what I am doing wrong.
- What I want is for example every product that starts with 01 or 02 goes to the food sector, products that start with 10 or 20 belong to clothes...
library(dplyr)
DF <- data.frame(Code = c(10105, 01112, 25441, 02422, 02552, 01010, 34552, 21120, 45210))
DF <- DF %>% mutate(Category = case_when(
substr(Code, 1, 2) %in% c(01, 02) ~ "Food",
substr(Code, 1, 2) %in% c(10, 20) ~ "Clothes",
substr(Code, 1, 2) %in% c(34, 45 ) ~ "Toiletries",
TRUE ~ "Unknown"
))
DF