get_data_from_formula: Extracts design matrix from a formula and data.

View source: R/util.R

get_data_from_formulaR Documentation

Extracts design matrix from a formula and data.

Description

This function takes a formula and a data frame, and returns a design matrix based on the right-hand side of the formula. If the formula is '~.', it treats all columns in the data as the design matrix. Otherwise, it constructs a design matrix without an intercept based on the specified formula.

Usage

get_data_from_formula(form, data)

Arguments

form

A formula object, e.g., '~ x1 + x2' or '~.'.

data

A data frame or matrix from which to extract the design matrix.

Value

A numeric matrix representing the design matrix.

Examples

data(mtcars)
# Extract a design matrix for 'mpg' and 'cyl'
X1 <- get_data_from_formula(~ mpg + cyl, mtcars)
# Extract a design matrix for all columns
X2 <- get_data_from_formula(~., mtcars)
# Extract a design matrix for 'hp'
X3 <- get_data_from_formula(~hp, mtcars)

ngme2 documentation built on May 20, 2026, 9:10 a.m.