library(dplyr, warn.conflicts = FALSE)
df <- tibble(
f1 = factor(c("a", "a", "a", "b", "b"), levels = c("a", "b", "c")),
f2 = factor(c("d", "e", "d", "e", "f"), levels = c("d", "e", "f")),
x = c(1, 1, 1, 2, 2),
y = 1:5
)
df %>%
group_by(f1, f2) %>%
filter(y > 3)
#> # A tibble: 2 x 4
#> # Groups: f1, f2 [9]
#> f1 f2 x y
#> <fct> <fct> <dbl> <int>
#> 1 b e 2 4
#> 2 b f 2 5
df %>%
group_by(f1, f2) %>%
filter(y > 3) %>%
summarise(n = n())
#> # A tibble: 9 x 3
#> # Groups: f1 [3]
#> f1 f2 n
#> <fct> <fct> <int>
#> 1 a d 0
#> 2 a e 0
#> 3 a f 0
#> 4 b d 0
#> 5 b e 1
#> 6 b f 1
#> 7 c d 0
#> 8 c e 0
#> 9 c f 0
df %>%
group_by(x) %>%
filter(y > 3)
#> # A tibble: 2 x 4
#> # Groups: x [2]
#> f1 f2 x y
#> <fct> <fct> <dbl> <int>
#> 1 b e 2 4
#> 2 b f 2 5
df %>%
group_by(x) %>%
filter(y > 3) %>%
summarise(n = n())
#> # A tibble: 2 x 2
#> x n
#> <dbl> <int>
#> 1 1 0
#> 2 2 2
g <- df %>%
group_by(x)
# corrupt the grouping structure
attributes(g)$groups <- attributes(g)$groups[1, ]
# the result is bad
g %>%
summarise(n = n())
#> # A tibble: 1 x 2
#> x n
#> <dbl> <int>
#> 1 1 3
g %>%
mutate(n = n())
#> # A tibble: 5 x 5
#> # Groups: x [2]
#> f1 f2 x y n
#> <fct> <fct> <dbl> <int> <int>
#> 1 a d 1 1 3
#> 2 a e 1 2 3
#> 3 a d 1 3 3
#> 4 b e 2 4 0
#> 5 b f 2 5 73211632
Created on 2018-12-11 by the reprex package (v0.2.1)