Skip to contents

Create a data frame with grouping attributes.

Usage

group_by(data, group_cols, add = FALSE, drop = TRUE)

Arguments

data

A data frame.

group_cols

A character vector of column names to group by.

add

TRUE or FALSE. Defaults to FALSE, overwriting any existing groupings. If TRUE variables are added to the groupings.

drop

TRUE or FALSE. Whether or not to drop empty groups. Defaults to TRUE.

Value

group_df

Details

Subset operations $<-, [[<-, [<-, and [ on a group_df will drop the grouping attributes. List-columns cannot be grouped on.

Examples

#----------------------------------------------------------------------------
# group_by() examples
#----------------------------------------------------------------------------
library(bkdat)

df <- data.frame(a = c(1, 2, 3), b = c(1, 1, 2), c = c(1, 2, 3))
group_by(df, "a")
#>   a b c
#> 1 1 1 1
#> 2 2 1 2
#> 3 3 2 3
group_by(df, "b")
#>   a b c
#> 1 1 1 1
#> 2 2 1 2
#> 3 3 2 3
group_by(df, c("a", "b"))
#>   a b c
#> 1 1 1 1
#> 2 2 1 2
#> 3 3 2 3

summarise(group_by(df, "b"), c(mean = "mean(a)"))
#>   b c(mean = "mean(a)")
#> 1 1             mean(a)
#> 2 2             mean(a)