to_matrix: Convert data to matrix

View source: R/utils.R

to_matrixR Documentation

Convert data to matrix

Description

Given an object creates a design matrix.

Usage

to_matrix(x, with_intercept = FALSE, remove_na = FALSE)

Arguments

x

(any) The object to be converted to matrix.

with_intercept

(logical(1)) Should be the "(Intercept)" column added at the beginning? FALSE by default.

remove_na

(logical(1)) Should NA values be removed?. FALSE by default.

Details

The following rules are applied when converting the object to matrix depending the object's type:

  • numeric vector: It is converted to a one column matrix.

  • character, logical or factor vectors: All of these are considered to be categorical variables and therefore a dummy matrix is created with all categories (or unique values) without the firts one, so a ⁠n x (categories_number - 1)⁠ matrix is created.

  • data.frame: All columns are included but character, logical or factor are included in the same way as described for vectors.

For all the columns a name is assigned if they has no one. The intercept is always added at the first column with the name "(Intercept)".

Value

A matrix.

Examples

## Not run: 
to_matrix(iris)
to_matrix(1:10)
to_matrix(c("a", "b", "c"))
to_matrix(data.frame(a = c("a", "b"), b = c("c", "d")))

## End(Not run)


brandon-mosqueda/SKM documentation built on Feb. 8, 2025, 5:24 p.m.