@nirgrahamuk , I tried to create reprex again and this time I see error with mutate(). I did create slightly bigger sample this time around with some possible combinations
# Sample Data
#Only Considering 2007 & 2018 to keep data rows within 200 rows limit.
df <- data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
Date = c("2007-01-01","2007-01-01",
"2007-01-01","2007-01-01","2007-01-01","2007-01-01",
"2007-02-01","2007-02-01","2007-02-01",
"2007-02-01","2007-02-01","2007-02-01","2007-03-01",
"2007-03-01","2007-03-01","2007-03-01","2007-03-01",
"2007-03-01","2007-04-01","2007-04-01","2007-04-01",
"2007-04-01","2007-04-01","2007-04-01","2007-05-01",
"2007-05-01","2007-05-01","2007-05-01","2007-05-01",
"2007-05-01","2007-06-01","2007-06-01","2007-06-01",
"2007-06-01","2007-06-01","2007-06-01","2007-07-01",
"2007-07-01","2007-07-01","2007-07-01","2007-07-01",
"2007-07-01","2007-08-01","2007-08-01","2007-08-01",
"2007-08-01","2007-08-01","2007-08-01","2007-09-01",
"2007-09-01","2007-09-01","2007-09-01","2007-09-01",
"2007-09-01","2007-10-01","2007-10-01","2007-10-01",
"2007-10-01","2007-10-01","2007-10-01","2007-11-01",
"2007-11-01","2007-11-01","2007-11-01",
"2007-11-01","2007-11-01","2007-12-01","2007-12-01",
"2007-12-01","2007-12-01","2007-12-01","2007-12-01",
"2018-01-01","2018-01-01","2018-01-01","2018-01-01",
"2018-01-01","2018-01-01","2018-01-01","2018-01-01",
"2018-02-01","2018-02-01","2018-02-01","2018-02-01",
"2018-02-01","2018-02-01","2018-02-01","2018-02-01",
"2018-03-01","2018-03-01","2018-03-01","2018-03-01",
"2018-03-01","2018-03-01","2018-03-01","2018-03-01",
"2018-03-01","2018-04-01","2018-04-01","2018-04-01",
"2018-04-01","2018-04-01","2018-04-01","2018-04-01",
"2018-04-01","2018-05-01","2018-05-01","2018-05-01",
"2018-05-01","2018-05-01","2018-05-01","2018-05-01",
"2018-05-01","2018-06-01","2018-06-01","2018-06-01",
"2018-06-01","2018-06-01","2018-06-01",
"2018-06-01","2018-06-01","2018-06-01","2018-07-01",
"2018-07-01","2018-07-01","2018-07-01","2018-07-01",
"2018-07-01","2018-07-01","2018-07-01","2018-07-01",
"2018-08-01","2018-08-01","2018-08-01","2018-08-01",
"2018-08-01","2018-08-01","2018-08-01","2018-08-01",
"2018-09-01","2018-09-01","2018-09-01","2018-09-01",
"2018-09-01","2018-09-01","2018-09-01","2018-09-01",
"2018-10-01","2018-10-01","2018-10-01","2018-10-01",
"2018-10-01","2018-10-01","2018-10-01","2018-10-01",
"2018-10-01","2018-11-01","2018-11-01","2018-11-01",
"2018-11-01","2018-11-01","2018-11-01","2018-11-01",
"2018-11-01","2018-11-01","2018-11-01","2018-12-01",
"2018-12-01","2018-12-01","2018-12-01","2018-12-01",
"2018-12-01","2018-12-01","2018-12-01",
"2018-12-01","2018-12-01","2018-12-01","2018-12-01"),
Country = c("Canada","Canada",
"Canada","USA","USA","USA","Canada","Canada","Canada",
"USA","USA","USA","Canada","Canada","Canada","USA",
"USA","USA","Canada","Canada","Canada","USA",
"USA","USA","Canada","Canada","Canada","USA","USA",
"USA","Canada","Canada","Canada","USA","USA",
"USA","Canada","Canada","Canada","USA","USA","USA",
"Canada","Canada","Canada","USA","USA","USA",
"Canada","Canada","Canada","USA","USA","USA","Canada",
"Canada","Canada","USA","USA","USA","Canada",
"Canada","Canada","USA","USA","USA","Canada",
"Canada","Canada","USA","USA","USA","Canada","Canada",
"Canada","Canada","USA","USA","USA","USA",
"Canada","Canada","Canada","Canada","USA","USA","USA",
"USA","Canada","Canada","Canada","Canada","USA",
"USA","USA","USA","USA","Canada","Canada","Canada",
"Canada","USA","USA","USA","USA","Canada",
"Canada","Canada","Canada","USA","USA","USA","USA",
"Canada","Canada","Canada","Canada","USA","USA",
"USA","USA","USA","Canada","Canada","Canada","Canada",
"USA","USA","USA","USA","USA","Canada","Canada",
"Canada","Canada","USA","USA","USA","USA",
"Canada","Canada","Canada","Canada","USA","USA","USA",
"USA","Canada","Canada","Canada","Canada","USA",
"USA","USA","USA","USA","Canada","Canada",
"Canada","Canada","USA","USA","USA","USA","USA","USA",
"Canada","Canada","Canada","Canada","Canada","USA",
"USA","USA","USA","USA","USA","USA"),
Category = c("BM","SS","TM","BM",
"SS","TM","BM","SS","TM","BM","SS","TM","BM","SS",
"TM","BM","SS","TM","BM","SS","TM","BM","SS",
"TM","BM","SS","TM","BM","SS","TM","BM","SS",
"TM","BM","SS","TM","BM","SS","TM","BM","SS",
"TM","BM","SS","TM","BM","SS","TM","BM","SS",
"TM","BM","SS","TM","BM","SS","TM","BM","SS",
"TM","BM","SS","TM","BM","SS","TM","BM","SS","TM",
"BM","SS","TM","BM","BM","SS","TM","BM","BM",
"SS","TM","BM","BM","SS","TM","BM","BM","SS",
"TM","BM","BM","SS","TM","BM","BM","SS","SS",
"TM","BM","BM","SS","TM","BM","BM","SS","TM",
"BM","BM","SS","TM","BM","BM","SS","TM","BM",
"BM","SS","TM","BM","BM","BM","SS","TM","BM","BM",
"SS","TM","BM","BM","SS","SS","TM","BM","BM",
"SS","TM","BM","BM","SS","TM","BM","BM","SS",
"TM","BM","BM","SS","TM","BM","BM","SS","TM",
"BM","BM","SS","SS","TM","BM","BM","SS","TM",
"BM","BM","BM","SS","SS","TM","BM","BM","BM",
"SS","TM","BM","BM","BM","BM","SS","SS","TM"),
Type = c("FBF","FSS","MTF","FBF",
"FSS","MTF","FBF","FSS","MTF","FBF","FSS","MTF",
"FBF","FSS","MTF","FBF","FSS","MTF","FBF",
"FSS","MTF","FBF","FSS","MTF","FBF","FSS","MTF",
"FBF","FSS","MTF","FBF","FSS","MTF","FBF","FSS",
"MTF","FBF","FSS","MTF","FBF","FSS","MTF","FBF",
"FSS","MTF","FBF","FSS","MTF","FBF","FSS","MTF",
"FBF","FSS","MTF","FBF","FSS","MTF","FBF","FSS",
"MTF","FBF","FSS","MTF","FBF","FSS","MTF","FBF",
"FSS","MTF","FBF","FSS","MTF",
"Freestanding Multidoor 3 Door CD","Freestanding Multidoor 3 Doors",
"FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD","Freestanding Multidoor 3 Doors",
"FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","Built-in Side by Side",
"FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD","Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","N/A Freestanding Multidoor / French Door",
"FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","Built-in Side by Side","FSS","MTF",
"Freestanding Multidoor 3 Door CD","Freestanding Multidoor 3 Doors",
"FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD","Freestanding Multidoor 3 Doors",
"Built-in Side by Side","FSS","MTF","Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","FSS","MTF",
"Freestanding Multidoor 3 Door CD",
"Freestanding Multidoor 3 Doors","Freestanding Multidoor 4 Doors",
"Built-in Side by Side","FSS","MTF",
"Freestanding Multidoor 3 Door CD","Freestanding Multidoor 3 Doors",
"Freestanding Multidoor 4 Door CD","FSS","MTF",
"Freestanding Multidoor 3 Door CD","Freestanding Multidoor 3 Doors",
"Freestanding Multidoor 4 Door CD",
"Freestanding Multidoor 4 Doors","Built-in Side by Side","FSS","MTF"),
`Sales/Value` = c("Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales","Sales","Sales",
"Sales","Sales","Sales","Sales"),
Sales = c(599,494,12405,2527,
21340,85437,704,548,13458,2753,16114,77410,1444,
788,14971,3997,26401,127672,1000,662,19306,3306,
29280,118265,1945,657,21066,4985,40786,119898,
2293,734,22281,4283,39411,132545,2473,988,23612,
3995,36324,146997,2790,1268,25277,6796,34440,
140139,2160,1000,22869,10091,50981,125366,2368,
857,24131,13340,45063,118031,2958,807,18723,
14987,39653,119328,2145,557,19435,8735,36685,89365,
439,385,151,8133,2251,13578,37775,93173,363,
434,139,9070,3191,12446,38431,99606,411,434,286,
9314,3987,16617,1,60131,139088,494,738,265,
8410,3110,13480,43046,126176,568,452,227,11072,
5886,18735,70588,149781,565,462,431,13645,5754,
16446,0,66483,168435,590,471,253,12768,4900,
11426,0,54272,111584,476,668,226,14349,4970,
14874,58666,159624,744,563,368,13005,9791,14027,
48159,134546,648,417,833,11123,9495,25121,8,
75378,139355,541,623,226,10735,9286,17687,557,2,
47047,117296,529,640,5,137,8208,6601,12610,28,
118,19,30552,96270)
)
df <-df%>%
mutate(Date = ymd(Date))
StartDate <- as.Date("2007-01-01")
EndDate <- as.Date("2019-07-01")
df <- df%>%
mutate(Sales = as.numeric(Sales))
(dist_df <- distinct(df,
Country, Category, Type, `Sales/Value`))
dfComplete_2 <- expand_grid(
Date = seq.Date(StartDate,EndDate,by="month"),
dist_df
) |> left_join(df |>group_by(Date,Country, Category, Type, `Sales/Value`) |>
summarize(`Sales` =sum(`Sales`)) |>
ungroup()) |> mutate(`Sales`=if_else(condition = is.na(`Sales`),true = 0L,false = `Sales`)) |>
arrange(`Sales/Value`, `Type`, `Category`, Country, Date)