25.13 Common use cases

summary6 <- function(data, var) {
  data |> summarize(
    min = min({{ var }}, na.rm = TRUE),
    mean = mean({{ var }}, na.rm = TRUE),
    median = median({{ var }}, na.rm = TRUE),
    max = max({{ var }}, na.rm = TRUE),
    n = n(),
    n_miss = sum(is.na({{ var }})),
    .groups = "drop"
  )
}

diamonds |>
  summary6(carat)
## # A tibble: 1 × 6
##     min  mean median   max     n n_miss
##   <dbl> <dbl>  <dbl> <dbl> <int>  <int>
## 1   0.2 0.798    0.7  5.01 53940      0
diamonds |> 
  group_by(cut) |> 
  summary6(carat)
## # A tibble: 5 × 7
##   cut         min  mean median   max     n n_miss
##   <ord>     <dbl> <dbl>  <dbl> <dbl> <int>  <int>
## 1 Fair       0.22 1.05    1     5.01  1610      0
## 2 Good       0.23 0.849   0.82  3.01  4906      0
## 3 Very Good  0.2  0.806   0.71  4    12082      0
## 4 Premium    0.2  0.892   0.86  4.01 13791      0
## 5 Ideal      0.2  0.703   0.54  3.5  21551      0