in the following dataframe
structure(list(model = c("A1", "A1", "B4", "B4", "B4", "A4",
"A4", "A4", "G4", "G4"), category = c("X", "Y", "X", "Y", "Z",
"X", "Y", "Z", "X", "Z"), sale = c(194L, 0L, 59L, 29L, 0L, 176L,
88L, 0L, 87L, 44L)), class = "data.frame", row.names = c(NA,
-10L))
model category sale
1 A1 X 194
2 A1 Y 0
3 B4 X 59
4 B4 Y 29
5 B4 Z 0
6 A4 X 176
7 A4 Y 88
8 A4 Z 0
9 G4 X 87
10 G4 Z 44
category variable includes uniques: X, Y or Z. I need to create all the possible combinations of model and category variables; some of them already exists but for example, comb. of: A1 - Z is missing. Therefore, I need to complete the table with missing combinations.
The sale columns need to follow the given rule:
- If a combination with Z is missing (e.g. A1-Z), sale is the same as model-Y (so A1-Y)
- If a combination with Y is missing (e.g. A1-Y), sale is the same as model-X (so A1-X)
Expected output:
model category sale
A1 X 194
A1 Y 0
A1 Z 0
B4 X 59
B4 Y 29
B4 Z 0
A4 X 176
A4 Y 88
A4 Z 0
G4 X 87
G4 Z 44
G4 Y 87