formula_term_labels: Retrieve term labels from a formula

View source: R/formula_utils.R

formula_term_labelsR Documentation

Retrieve term labels from a formula

Description

This function extracts the term labels from the right-hand side of a given R formula. If an intercept is to be included (and a name for the intercept is provided), it will be added as the first element of the returned vector.

Usage

formula_term_labels(formula, intercept = "(Intercept)")

Arguments

formula

An R formula, e.g. ~ x1 * x2.

intercept

A character string indicating the name for the intercept. The default value is "(Intercept)". If NULL is provided, the intercept will not be included, even if present in the formula.

Details

The default intercept value, "(Intercept)", is chosen to be consistent with the intercept label returned by functions such as stats::lm(), stats::model.matrix(), and similar modeling functions.

Value

A character vector containing the term labels. If an intercept is present and intercept is not NULL, the intercept is returned first, followed by the remaining terms.

Note

This function is documented by ChatGPT after some discussion.

Examples

# With intercept:
formula_term_labels(~ x1 * x2)

# Without intercept:
formula_term_labels(~ x1 * x2, intercept = NULL)


SSBtools documentation built on June 19, 2025, 5:07 p.m.