Sort products into groups

  • Hi guys, I have a lot of codes and want to sort them into three different groups, but I don't want to put the entire code of the product. Example: I have 10105, 1112, 25441, 2422, 4552 they belong to the food sector, but instead of using all the codes, I want to say that when 1 or 2 appears at the beginning, designate as belonging to the food sector
  • I know if I use case when and all the code I can do it, but I want to use just the first number of each code

Here is one method. I invented that an initial digit of 3 or 4 designates the sector Clothes.

library(dplyr)

DF <- data.frame(Code = c(10105, 1112, 25441, 2422, 4552))

DF <- DF %>% mutate(Category = case_when(
  substr(Code, 1, 1) %in% c(1,2) ~ "Food",
  substr(Code, 1, 1) %in% c(3,4) ~ "Clothes",
  TRUE ~ "Unknown"
))
DF  
#>    Code Category
#> 1 10105     Food
#> 2  1112     Food
#> 3 25441     Food
#> 4  2422     Food
#> 5  4552  Clothes

Created on 2022-04-21 by the reprex package (v0.2.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.