0

Say, I have the following data:

set.seed(10)
dat <- data_frame(
  SKU = rep(c("sku1", "sku2", "sku3"), each=4),
  invoicedate = rep(c("12/01/2019", "12/02/2019", "12/03/2019", "12/04/2019"), 3),
  cantidad = round(rnorm(12, mean = 80, sd=4), 0)
)

dat

How do I get to the following format?:

invoicedate sku1 sku2 sku3
12/01/2019  80    81   73
12/02/2019  79    82   79
12/03/2019  75    75   84
12/04/2019  78    79   83

Thank you!

  • 1
    Try `library(tidyr);spread(dat, SKU, cantidad)` – akrun May 02 '19 at 18:45
  • I tried that before posting the question. It works on "dat" (the example dataset here) but introduces a lot of NAs in the actual dataset with 3 years of data and 1200+ SKUs. – States.the.Obvious May 02 '19 at 18:52
  • If the original data doesn't have that particular combination, then by default it would be `NA` Or else you may need to do a `complete`, impute those missing values and then `spread` – akrun May 02 '19 at 18:52
  • I did the complete and fill before "spread" but it is still filling in the NAs. I don't understand why. I thought that my code was wrong that's why I posted. I should just delete the question. – States.the.Obvious May 02 '19 at 18:56
  • `complete` will just give the whole combination, but still if you don't have a value for that combination, the row in that other column will be NA – akrun May 02 '19 at 18:59

0 Answers0