Skip to content

Instantly share code, notes, and snippets.

View maurolepore's full-sized avatar

Mauro Lepore maurolepore

View GitHub Profile
@maurolepore
maurolepore / summarise_with.R
Created August 17, 2020 16:33 — forked from vintented/summarise_with.R
Thanks for the suggestion. I needed to added the .group parameter because otherwise some functions like sum will perhaps not behave as intended. Interested to hear your thoughts.
library(dplyr, warn.conflicts = FALSE)
library(rlang)
summarise_with <- function(.data, ..., .f, .with, .over, .group = TRUE, na.rm = TRUE) {
stopifnot(rlang::is_logical(.group))
if(rlang::quo_is_symbol(rlang::enquo(.with))) {
.with <- rlang::enquo(.with)
} else {
@maurolepore
maurolepore / summarise_with.R
Created August 17, 2020 16:33 — forked from vintented/summarise_with.R
Thanks for the suggestion. I needed to added the .group parameter because otherwise some functions like sum will perhaps not behave as intended. Interested to hear your thoughts.
library(dplyr, warn.conflicts = FALSE)
library(rlang)
summarise_with <- function(.data, ..., .f, .with, .over, .group = TRUE, na.rm = TRUE) {
stopifnot(rlang::is_logical(.group))
if(rlang::quo_is_symbol(rlang::enquo(.with))) {
.with <- rlang::enquo(.with)
} else {
# What's the most natural way to express this code in base R?
library(dplyr, warn.conflicts = FALSE)
mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(disp), n = n())
#> # A tibble: 3 x 3
#> cyl mean n
#> <dbl> <dbl> <int>
#> 1 4 105. 11
#> 2 6 183. 7