1

I have a dataset:

structure(list(CC = c("Bagel Shop", "Bagel Shop", "Bagel Shop", 
"Bagel Shop", "Bagel Shop", "Bagel Shop", "Bagel Shop", "Bagel Shop", 
"Bagel Shop", "Bagel Shop"), `End Market` = c("Food Service", 
"Food Service", "Food Service", "Food Service", "Food Service", 
"Food Service", "Food Service", "Food Service", "Food Service", 
"Food Service"), Entity = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L), SBPHYP = c(201901L, 201903L, 201904L, 201905L, 201907L, 
201908L, 201909L, 201910L, 201911L, 201912L), SBYEAR = c(2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L
), SBMONTH = c(1L, 3L, 4L, 5L, 7L, 8L, 9L, 10L, 11L, 12L), `Work Days` = c(21L, 
21L, 21L, 22L, 22L, 22L, 20L, 23L, 19L, 21L)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
    CC = c("Bagel Shop", "Bagel Shop", "Bagel Shop", "Bagel Shop", 
    "Bagel Shop", "Bagel Shop", "Bagel Shop", "Bagel Shop", "Bagel Shop", 
    "Bagel Shop"), `End Market` = c("Food Service", "Food Service", 
    "Food Service", "Food Service", "Food Service", "Food Service", 
    "Food Service", "Food Service", "Food Service", "Food Service"
    ), SBMONTH = c(1L, 3L, 4L, 5L, 7L, 8L, 9L, 10L, 11L, 12L), 
    .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
        10L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))

and would like to expand my data with all 12 months for each Name. I use the following code

library(dplyr)
library(tidyverse)
df <- df %>%
  complete(CC, SBMONTH)

but getting the Error message -

"Error in `dplyr::summarise()`:
! Problem while computing `..1 = complete(data = dplyr::cur_data(), ..., fill = fill, explicit = explicit)`.
*i The error occurred in group 1: CC = "Bagel Shop", End Market = "Food Service", SBMONTH = 1.
Caused by error:
! object 'CC' not found*

With the sample

df <- data.frame("Customer Code" = c("Bagel Shop", "Bagel Shop", "Bagel Shop", "Butcher", "Butcher", "Butcher", "Butcher", "Cafeteria"), "SBMONTH" = c(1:8))
df <- df %>%
  complete(`Customer.Code`, SBMONTH)

everything works.

What's wrong? Is any other methods to do it?

Quinten
  • 35,235
  • 5
  • 20
  • 53
grislepak
  • 31
  • 3
  • Please try to use `dput()` to insert a workable R object. Also, you should read this guide on how to build [minimal examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Francesco Grossetti Oct 07 '22 at 13:19
  • When I import the data you shared (I deleted the second line and all the `|` characters and used `read.table()` on it), I ran `df %>% complete(Name, Month)`, and it worked fine. Could you (a) share data with `dput()` so it is copy/pasteable without edits and will include the class and structure information in case there are problems there--e.g., `dput(df[1:20, ])` for the first 20 rows, and (b) make sure that the sample of data you share illustrates the problem. – Gregor Thomas Oct 07 '22 at 13:34
  • When I run the prepared sample of code, it works good. But within whole code it doesn't work. I will edit my question with data structure And code – grislepak Oct 07 '22 at 13:58
  • I guess I found out the problem. Because I did some group_by() and mutate() manipulations before to run complete() I have to add the ```df <- as.data.frame(df)``` – grislepak Oct 07 '22 at 14:23

1 Answers1

1

You need to ungroup your dataframe

You are currently grouping by CC, and

With grouped data frames, complete() operates within each group. Because of this, you cannot complete a grouping column.

?complete

So to fix this just:

df |> 
  ungroup() |>
  complete(CC, SBMONTH)
Captain Hat
  • 2,444
  • 1
  • 14
  • 31
  • 1
    And, with previous data, even grouped, no entries started with `1`, but smallest value was `2`, largest `12`, so complete expanded to 2:12 for Names. i.e. complete doesn't know or care that SBMONTH is 'date-like', just that the range to 'expand' to is 2:12, in the prior instance. – Chris Oct 07 '22 at 17:09