Skip to contents

Returns a factor vector of grouping combinations. Return vector is same length as the number of rows in the data frame. levels of the factor can be lexically ordered or unordered.

Usage

group_levels(data, group_cols, arrange = TRUE)

Arguments

data

A data frame.

group_cols

A character vector of column names to group by.

arrange

TRUE or FALSE. If TRUE, then the factor levels are arranged lexically in ascending order. If false, factor levels are not ordered.

Value

factor

Examples


#----------------------------------------------------------------------------
# group_levels() examples
#----------------------------------------------------------------------------
group_levels(mtcars, "cyl")
#>  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
#> Levels: 4 6 8
group_levels(mtcars, c("cyl", "carb"))
#>  [1] 6|4 6|4 4|1 6|1 8|2 6|1 8|4 4|2 4|2 6|4 6|4 8|3 8|3 8|3 8|4 8|4 8|4 4|1 4|2
#> [20] 4|1 4|1 8|2 8|2 8|4 8|2 4|1 4|2 4|2 8|4 6|6 8|8 4|2
#> Levels: 4|1 4|2 6|1 6|4 6|6 8|2 8|3 8|4 8|8