Skip to contents

Adds column grouping information as a hidden attribute to a data frame. This allows you to specify which columns belong to the same group without modifying the data structure itself.

Usage

anno_col_group(data, groups, attr_name = "col_groups")

Arguments

data

A data frame or data.table to annotate.

groups

A named list or vector where names are group names and values are column names (or indices) that belong to that group. Supports both explicit column names and regular expressions.

attr_name

The name of the attribute to store the grouping information. Default is "col_groups". This allows you to have separate grouping attributes for different purposes (e.g., "col_groups_color" for color grouping, "col_groups_shape" for shape grouping).

Value

The input data frame with an additional attribute containing the grouping information.

Details

The grouping information is stored as a hidden attribute, which does not affect normal data frame operations. This allows you to annotate data with grouping information without changing its structure.

By default, the grouping information is stored in an attribute named "col_groups". However, you can specify a different attribute name to allow for separate grouping configurations for color and shape (or other purposes).

Examples

if (FALSE) { # \dontrun{
# Create a data frame with multiple columns
data <- data.frame(
  ID = c("A", "B", "C"),
  Asia_1 = c(0.5, 0.6, 0.7),
  Asia_2 = c(0.4, 0.5, 0.6),
  North_1 = c(0.3, 0.4, 0.5),
  North_2 = c(0.2, 0.3, 0.4)
)

# Method 1: Explicit column names
data <- anno_col_group(data, list(
  Asia = c("Asia_1", "Asia_2"),
  North = c("North_1", "North_2")
))

# Method 2: Regular expression
data <- anno_col_group(data, list(
  Asia = "^Asia",
  North = "^North"
))

# Method 3: Separate grouping for color and shape
data <- anno_col_group(data, list(
  Group1 = c("Asia_1", "Asia_2")
), attr_name = "col_groups_color")

data <- anno_col_group(data, list(
  Group1 = c("North_1")
), attr_name = "col_groups_shape")

# Access grouping information
attr(data, "col_groups_color")
attr(data, "col_groups_shape")
} # }