Thank you! Adding new reprex to make sure above solution can handle all groups. In my testing, I couldn't achieve the right solution. But it would be helpful, if some modifications can still work.
library(tidyverse)
df <- data.frame(
stringsAsFactors = FALSE,
ACCT_DT = c(202306L,202306L,
202306L,202306L,202306L,202306L,202306L,202306L,
202306L,202306L,202306L,202306L,202306L,
202306L,202306L,202307L,202307L,202307L,202307L,
202307L,202307L,202307L,202307L,202307L,202307L,
202307L,202307L,202308L,202308L,202308L,
202308L,202308L,202308L,202308L,202308L,202308L,
202308L,202308L,202308L,202308L,202309L,202309L,
202309L,202309L,202309L,202309L,202309L,
202309L,202309L,202309L,202309L,202309L,202309L,
202310L,202310L,202310L,202310L,202310L,202310L,
202310L,202310L,202310L,202310L,202310L),
Country = c("USA","USA","USA",
"USA","USA","CA","CA","CA","CA","CA","CA",
"CA","CA","CA","CA","USA","USA","USA","CA",
"CA","CA","CA","CA","CA","CA","CA","CA",
"USA","USA","USA","CA","CA","CA","CA","CA",
"CA","CA","CA","CA","CA","USA","USA","USA",
"CA","CA","CA","CA","CA","CA","CA","CA","CA",
"CA","USA","USA","CA","CA","CA","CA","CA",
"CA","CA","CA","CA"),
Category = c("Soft Goods",
"Soft Goods","Soft Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods","Soft Goods",
"Soft Goods","Soft Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods","Hard Goods",
"Soft Goods","Soft Goods","Soft Goods",
"Hard Goods","Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods","Soft Goods",
"Soft Goods","Soft Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Soft Goods","Soft Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods",
"Hard Goods","Hard Goods","Hard Goods","Hard Goods",
"Hard Goods"),
Type = c("A","C","A","B",
"A","A","A","A","A","C","C","A","A","A1",
"B","A","C","A","A","A","A","A","C","A",
"A","A1","B","A","C","A","A","A","A","A",
"C","C","A","A","A1","B","C","A","A","A",
"A","A","A","C","C","A","A","A1","B","C",
"A","A","A","A","A","C","A","A","A1","B"),
Code = c("Code_1","Exists",
"Exists","Code_1","Code_1","Code_1","Code_2",
"Code_3","Code_4","Exists","Code","Exists",
"Non Exists","Exists","Exists","Code_3","Exists",
"Exists","Code_1","Code_2","Code_3","Code_4",
"Exists","Exists","Non Exists","Exists","Exists",
"Code_3","Exists","Exists","Code_1","Code_2",
"Code_3","Code_4","Exists","Code","Exists",
"Non Exists","Exists","Exists","Exists","Exists",
"Non Exists","Code_1","Code_2","Code_3",
"Code_4","Exists","Code","Exists","Non Exists",
"Exists","Exists","Exists","Exists","Code_1",
"Code_2","Code_3","Code_4","Exists","Exists",
"Non Exists","Exists","Exists"),
Sales = c(6,5,14,15,10,
23,25,28,22,63,2,331.83,7,18,6,6,5,13,
11,24,21,25,64,319.83,22,19,6,6,5,13,10,
11,22,20,63,4,321.83,24,17,6,5,12,6,3,
9,12,20,67,2,329.19,20,16,7,7,19,34,3,
10,11,68,329.19,22,14,7)
)
# All Codes per Type
codes_by_type <- df %>%
distinct(Country, Category, Type, Code)