The “curly operator” was added in rlang
a few years ago, and I have yet to really use it much. It came in handy during a data analyst consulting gig I had during the summer of 2021, and I should use it more.
library ("palmerpenguins" )
library ("tidyverse" )
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0 ✔ purrr 1.0.1
✔ tibble 3.1.8 ✔ dplyr 1.0.10
✔ tidyr 1.2.1 ✔ stringr 1.5.0
✔ readr 2.1.3 ✔ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
For instance, can I make a helper function to reduce typing out the same few lines of code that I use often?
summary_stats <- function (data_frame, grouping_variable, numerical_variable){
data_frame |>
filter (! is.na ({{grouping_variable}})) |>
group_by ({{grouping_variable}}) |>
summarize (min = min ({{numerical_variable}}, na.rm = TRUE ),
xbar = mean ({{numerical_variable}}, na.rm = TRUE ),
med = median ({{numerical_variable}}, na.rm = TRUE ),
s = sd ({{numerical_variable}}, na.rm = TRUE ),
max = max ({{numerical_variable}}, na.rm = TRUE ))
}
summary_stats (penguins, species, bill_length_mm)
# A tibble: 3 × 6
species min xbar med s max
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Adelie 32.1 38.8 38.8 2.66 46
2 Chinstrap 40.9 48.8 49.6 3.34 58
3 Gentoo 40.9 47.5 47.3 3.08 59.6
summary_stats (penguins, island, body_mass_g)
# A tibble: 3 × 6
island min xbar med s max
<fct> <int> <dbl> <dbl> <dbl> <int>
1 Biscoe 2850 4716. 4775 783. 6300
2 Dream 2700 3713. 3688. 417. 4800
3 Torgersen 2900 3706. 3700 445. 4700