Skip to content

Instantly share code, notes, and snippets.

@ibecav
Created January 5, 2023 14:19
Show Gist options
  • Save ibecav/0fbc157d5cfd7c5b527c0dbe8d244db3 to your computer and use it in GitHub Desktop.
Save ibecav/0fbc157d5cfd7c5b527c0dbe8d244db3 to your computer and use it in GitHub Desktop.
Summarise some columns
summarise_columns <-
function(dfname, ...) {
checkmate::assert(
checkmate::checkClass(dfname, "tbl_lazy"),
checkmate::checkClass(dfname, "data.frame")
)
grps <- enquos(...)
# print(as.character(grps))
if ("tbl_lazy" %in% class(dfname)) {
summary_df <-
dfname %>%
count(!!!grps, name = "Count") %>%
collect()
} else {
summary_df <-
dfname %>%
group_by(!!!grps, .drop = FALSE) %>%
summarise(Count = n())
}
summary_df %>%
mutate(Pct = Count / sum(Count) * 100) %>%
ungroup() %>%
arrange(!!!grps[1:length(grps) - 1], desc(Pct))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment