| group_by | R Documentation |
Determine the groups within a data.frame to perform operations on. ungroup() removes the grouping levels.
group_by(.data, ..., .add = FALSE, .drop = group_by_drop_default(.data))
ungroup(x, ...)
.data |
|
... |
One or more unquoted column names to group/ungroup the data by. |
.add |
|
.drop |
|
x |
A |
When using group_by(), a data.frame, grouped by the grouping variables.
When using ungroup(), a data.frame.
On grouped data, mutate(), transmute(), filter(), arrange(), slice(), select(), rename(), distinct()
and the other verbs which modify their input keep its class, including a subclass of a grouped data.frame added by
another package. This differs from dplyr 1.0.10, which drops such a subclass. A method for an extension class
which calls NextMethod(), e.g. mutate.myclass <- function(.data, ...) NextMethod(), gets back its own class and
should not add it again. summarise() and count() build a new data.frame, so their result does not keep the
subclass.
group_by(mtcars, am, cyl)
ungroup(mutate(group_by(mtcars, am, cyl), sumMpg = sum(mpg)))
mtcars %>%
group_by(am, cyl) %>%
mutate(sumMpg = sum(mpg)) %>%
ungroup()
mtcars %>%
group_by(carb) %>%
filter(any(gear == 5))
# You can group by expressions: this is just short-hand for
# a mutate() followed by a group_by()
mtcars %>% group_by(vsam = vs + am)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.