I have this data:
dat
month item sales
1 a 10
2 c 20
2 b 5
3 a 3
As you can see, there are some missings. Not every item is listed in a month since there were no sales. I want to complete the data and fill the missings values in sales with 0.
The result should look like this:
month item sales
1 a 10
1 b 0
1 c 0
2 a 0
2 b 5
2 c 20
3 a 3
3 b 0
3 c 0
Is there a way to add the missing combinations to a data frame and impute the missings with 0.
structure(list(month = c(1, 2, 2, 3), item = structure(c(2L,
3L, 4L, 1L), .Label = c("a", "A", "b", "c"), class = "factor"),
sales = c(10, 20, 5, 3)), class = "data.frame", row.names = c(NA,
-4L))