2

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:

  1. If a combination with Z is missing (e.g. A1-Z), sale is the same as model-Y (so A1-Y)
  2. 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
Mark Noble
  • 21
  • 2

1 Answers1

0

Kinda quick and dirty way to do it.

df %>% 
  complete(model, category) %>%  
  mutate(sale = if_else(is.na(sale), lag(sale), sale))

# A tibble: 12 × 3
   model category  sale
   <chr> <chr>    <int>
 1 A1    X          194
 2 A1    Y            0
 3 A1    Z            0
 4 A4    X          176
 5 A4    Y           88
 6 A4    Z            0
 7 B4    X           59
 8 B4    Y           29
 9 B4    Z            0
10 G4    X           87
11 G4    Y           87
12 G4    Z           44
Chamkrai
  • 5,912
  • 1
  • 4
  • 14