Created
June 29, 2020 07:17
-
-
Save romainfrancois/afebd3dd7aaf861d094e8be6ab18778d to your computer and use it in GitHub Desktop.
code used in my dplyr talk at e-Rum 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr, warn.conflicts = FALSE) | |
library(palmerpenguins) | |
# multiple rows | |
penguins %>% | |
group_by(species) %>% | |
summarise( | |
prob = c(.25, .75), | |
length = quantile(bill_length_mm, prob, na.rm = TRUE), | |
depth = quantile(bill_depth_mm, prob, na.rm = TRUE) | |
) | |
# multiple columns | |
penguins %>% | |
group_by(species) %>% | |
summarise( | |
broom::tidy(lm(bill_depth_mm ~ bill_length_mm)) | |
) | |
# working across() columns | |
penguins %>% | |
group_by(species) %>% | |
summarise( | |
across(starts_with("bill"), min, na.rm = TRUE) | |
) | |
# working across() columns | |
penguins %>% | |
group_by(species) %>% | |
summarise( | |
across(starts_with("bill"), | |
list(min = min, max = max), | |
na.rm = TRUE | |
) | |
) | |
# .groups = | |
penguins %>% | |
group_by(species, island) %>% | |
summarise( | |
prob = c(.25, .75), | |
length = quantile(bill_length_mm, prob, na.rm = TRUE), | |
depth = quantile(bill_depth_mm, prob, na.rm = TRUE) | |
) | |
penguins %>% | |
group_by(species, island) %>% | |
summarise( | |
length = mean(bill_length_mm, na.rm = TRUE), | |
depth = mean(bill_depth_mm, na.rm = TRUE) | |
) | |
penguins %>% | |
group_by(species, island) %>% | |
summarise( | |
length = mean(bill_length_mm, na.rm = TRUE), | |
depth = mean(bill_depth_mm, na.rm = TRUE) | |
) | |
# .groups = drop | |
penguins %>% | |
group_by(species, island) %>% | |
summarise(.groups = "drop", | |
length = mean(bill_length_mm, na.rm = TRUE), | |
depth = mean(bill_depth_mm, na.rm = TRUE) | |
) | |
# .groups = keep | |
penguins %>% | |
group_by(species, island) %>% | |
summarise(.groups = "keep", | |
length = mean(bill_length_mm, na.rm = TRUE), | |
depth = mean(bill_depth_mm, na.rm = TRUE) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment