summarise2 | R Documentation |
Wrapper on dplyr::summarise
that sets the default for the .group variable
to "keep". This means that all the groups set in dplyr::group_by
are
retained, not just the first group.
summarise2(.data, ..., .by = NULL, .groups = "keep")
summarize2(.data, ..., .by = NULL, .groups = "keep")
An object usually of the same type as .data
.
The rows come from the underlying group_keys()
.
The columns are a combination of the grouping keys and the summary expressions that you provide.
The grouping structure is controlled by the .groups=
argument, the
output may be another grouped_df, a tibble or a rowwise data frame.
Data frame attributes are not preserved, because summarise()
fundamentally creates a new data frame.
Center: mean()
, median()
Spread: sd()
, IQR()
, mad()
Range: min()
, max()
,
Position: first()
, last()
, nth()
,
Count: n()
, n_distinct()
Logical: any()
, all()
The data frame backend supports creating a variable and using it in the
same summary. This means that previously created summary variables can be
further transformed or combined within the summary, as in mutate()
.
However, it also means that summary variables with the same names as previous
variables overwrite them, making those variables unavailable to later summary
variables.
This behaviour may not be supported in other backends. To avoid unexpected results, consider using new names for your summary variables, especially when creating multiple summaries.
This function is a generic, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.
The following methods are currently available in loaded packages: \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("summarise")}.
dplyr::summarise()
and dplyr::summarize()
df <- data.frame(
group = c("A", "A", "B", "B"),
id = c(1, 1, 2, 2),
value = c(10, 4, 20, 6)
)
# summarise2 doesn't produce message about groups
df |>
dplyr::group_by(group, id) |>
summarise2(mean = mean(value))
# summarise doesn't retain all the groups set in `group_by`
df |>
dplyr::group_by(group, id) |>
dplyr::summarise(mean = mean(value))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.