Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete doesn't work well with grouped data frames. #966

Closed
cimentadaj opened this issue May 18, 2020 · 4 comments · Fixed by #1289
Closed

complete doesn't work well with grouped data frames. #966

cimentadaj opened this issue May 18, 2020 · 4 comments · Fixed by #1289

Comments

@cimentadaj
Copy link

cimentadaj commented May 18, 2020

I just spent a few minutes figuring out why complete was generating all combination of values. This was happening because the data frame was grouped. Perhaps it would be useful to others to document this in the details section of complete.

library(dplyr, warn.conflicts = FALSE)
library(tidyr)
df <- tibble(
  group = c(1:2, 1),
  item_id = c(1:2, 2),
  item_name = c("a", "b", "b"),
  value1 = 1:3,
  value2 = 4:6
)

# Expected
df %>% complete(group, item_id, item_name)
#> # A tibble: 8 x 5
#>   group item_id item_name value1 value2
#>   <dbl>   <dbl> <chr>      <int>  <int>
#> 1     1       1 a              1      4
#> 2     1       1 b             NA     NA
#> 3     1       2 a             NA     NA
#> 4     1       2 b              3      6
#> 5     2       1 a             NA     NA
#> 6     2       1 b             NA     NA
#> 7     2       2 a             NA     NA
#> 8     2       2 b              2      5

# Not expected
df %>%
  group_by(group, item_id, item_name) %>%
  complete(group, item_id, item_name)
#> # A tibble: 3 x 5
#> # Groups:   group, item_id, item_name [3]
#>   group item_id item_name value1 value2
#>   <dbl>   <dbl> <chr>      <int>  <int>
#> 1     1       1 a              1      4
#> 2     1       2 b              3      6
#> 3     2       2 b              2      5
@Sibojang9
Copy link

I could not find clear documentation on the complete function, especially the use of grouping variables. Current official example showed only one grouping variable.

I tried complete(group,group2, nesting(item_id, item_name)) , which would run but ended up with surprising results. A more comprehensive guidance on complete is expected.

@monkeywithacupcake
Copy link

Related to #396

monkeywithacupcake added a commit to monkeywithacupcake/tidyr that referenced this issue Mar 20, 2021
add documentation to complete to explain current complete() behavior for grouped data. Closes tidyverse#966
@DavisVaughan DavisVaughan added grids #️⃣ expanding, nesting, crossing, ... group 👨‍👨‍👦‍👦 and removed grids #️⃣ expanding, nesting, crossing, ... labels Nov 10, 2021
@DavisVaughan DavisVaughan added the grids #️⃣ expanding, nesting, crossing, ... label Nov 19, 2021
@DavisVaughan
Copy link
Member

DavisVaughan commented Dec 20, 2021

Slightly more minimal reprex:

library(dplyr, warn.conflicts = FALSE)
library(tidyr)

df <- tibble(
  group1 = 1:2,
  group2 = 3:4
)

df %>% 
  expand(group1, group2)
#> # A tibble: 4 × 2
#>   group1 group2
#>    <int>  <int>
#> 1      1      3
#> 2      1      4
#> 3      2      3
#> 4      2      4

df %>% 
  group_by(group1, group2) %>%
  expand(group1, group2)
#> # A tibble: 2 × 2
#> # Groups:   group1, group2 [2]
#>   group1 group2
#>    <int>  <int>
#> 1      1      3
#> 2      2      4

Created on 2021-12-20 by the reprex package (v2.0.1)

@DavisVaughan
Copy link
Member

This issue in particular is really a documentation issue. complete() on a grouped-df operates within each group. #1289 added an example with a grouped-df and a section in the docs detailing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment