Compute a summary for groups with the total included.
Source:R/dplyr-plus.R
summarise_with_total.Rd
This function is useful to create end tables, apply the same formula to a group and to its overall.
You can specify a personalized Total
value with the .label
argument. You
You should only use the output from summarise_with_total()
with tidyr::pivot_wider()
,
write data to a spreadsheet, gt::gt()
after that. Don't try to do more computing afterwards.
It can also be used for plotting
Changes the .by
variable to a factor.
Arguments
- .data
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.
- ...
<
data-masking
> Name-value pairs of summary functions. The name will be the name of the variable in the result.The value can be:
A vector of length 1, e.g.
min(x)
,n()
, orsum(is.na(y))
.A data frame, to add multiple columns from a single expression.
Returning values with size 0 or >1 was deprecated as of 1.1.0. Please use
reframe()
for this instead.- .by
-
<
tidy-select
> Optionally, a selection of columns to group by for just this operation, functioning as an alternative togroup_by()
. For details and examples, see ?dplyr_by. - .label
Label of the total value
- .first
Should the total be on top
Examples
# works with `.by`
mtcars |>
summarise_with_total(
x = mean(mpg),
.by = vs,
.label = "All vs"
)
#> vs x
#> 1 All vs 20.09062
#> 2 0 16.61667
#> 3 1 24.55714
# works with `group_by()`
mtcars |>
dplyr::group_by(vs) |>
summarise_with_total(
x = mean(mpg),
.label = "All vs"
)
#> # A tibble: 3 × 2
#> vs x
#> <fct> <dbl>
#> 1 All vs 20.1
#> 2 0 16.6
#> 3 1 24.6