This function adds a new column to a data frame with a specified name. The new column indicates groups based on the grouping variables provided. When no grouping variable is provided, a default value is assigned to the new column.

add_group_column(data, name, group_by = NULL)

Arguments

data

A data frame.

name

The name of the new column to be added.

group_by

A vector of strings specifying the column names used for grouping. Default is NULL.

Value

A data frame with the new column added.

Examples

if (FALSE) {
data <- data.frame(x = c(1,2,3,1,2,3), y = c("a","b","c","a","b","c"))
data %>% add_group_column(name = "group_id", group_by = c("x", "y"))
}