Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Last active May 14, 2022 02:35
Show Gist options
  • Save DavisVaughan/7004f857074bf65e482e54d766c56047 to your computer and use it in GitHub Desktop.
Save DavisVaughan/7004f857074bf65e482e54d766c56047 to your computer and use it in GitHub Desktop.
library(tidyr)
library(dplyr)
df <- tibble(
g1 = c("x", "x", "y", "y", "y"),
g2 = factor(c("a", "a", "a", "b", "a"), levels = c("a", "b", "c"))
)
df
df %>%
group_by(g1, g2, .drop = FALSE) %>%
summarise(n = n(), .groups = "drop")
df %>%
group_by(g1, g2) %>%
summarise(n = n(), .groups = "drop") %>%
complete(g1, g2, fill = list(n = 0L))
df %>%
group_by(g1, g2) %>%
group_data() %>%
complete(g1, g2, fill = list(.rows = vctrs::list_of(integer())))
df <- tibble(
e = 1,
f = factor(c(1, 1, NA, 2), levels = 1:3),
g = c(1, 2, 2, 2),
x = c(1, 2, 1, 4)
)
df %>%
group_by(e, f, g, .drop = FALSE) %>%
summarise(z = n())
df %>%
group_by(e, f, g) %>%
summarise(z = n(), .groups = "drop") %>%
complete(e, f, fill = list(z = 0L))
# Expanding the group data:
gd <- df %>%
group_by(e, f, g) %>%
group_data()
all <- gd %>%
expand(e, f)
matches <- vctrs::vec_locate_matches(all, gd[c("e", "f")])
lhs_done <- vctrs::vec_slice(all, matches$needles)
rhs_done <- vctrs::vec_slice(gd[c("g", ".rows")], matches$haystack)
rhs_done$.rows[vctrs::vec_equal_na(rhs_done$.rows)] <- vctrs::list_of(integer())
vctrs::vec_cbind(lhs_done, rhs_done)
mtcars %>%
mutate(am=as.factor(am)) %>%
group_by(gear, am, cyl, .drop=FALSE) %>%
summarise(count=n(), .groups="drop")
mtcars %>%
mutate(am=as.factor(am)) %>%
group_by(gear, am, cyl) %>%
summarise(count=n(), .groups="drop") %>%
complete(gear, am, fill = list(count = 0L))
df <- tibble(
e = 1,
f = factor(c(1, 1, 2, 2), levels = 1:3),
g = c(1, 1, 2, 2),
x = c(1, 2, 1, 4)
)
df
df %>%
group_by(e, g, f, .drop = FALSE) %>%
summarise(n = n(), .groups = "drop")
df %>%
group_by(e, g, f, .drop = TRUE) %>%
summarise(n = n(), .groups = "drop") %>%
complete(e, g, f, fill = list(n = 0L))
# packed tibble example, it seems nested factors dont expand.
# not sure what the intended behavior would be either way.
df <- tibble(
e = 1,
f = factor(c(1, 1, 2, 2), levels = 1:3),
g = tibble(x = factor(c(1, 1, 2, 2), levels = 1:3)),
x = c(1, 2, 1, 4)
)
df
df %>%
group_by(e, f, g, .drop = FALSE) %>%
summarise(n = n(), .groups = "drop")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment