strip_design_factor_names: Simplify the column names of a simple design matrix

View source: R/data_manip_utils.R

strip_design_factor_namesR Documentation

Simplify the column names of a simple design matrix

Description

The standard model.matrix() function generates column names for factor columns by concatenating the factor's name with the level. This helps guarantee non-duplicated column names in the general case, but for simple cases it results in overly long and complex column names. This function removes the factor names, leaving only the level names.

Usage

strip_design_factor_names(design, prefixes = names(attr(design, "contrasts")))

Arguments

design

The design matrix whose column names should be cleaned up (usually created by model.matrix()).

prefixes

A character vector of prefixes to be deleted from column names. Normally, this argument is set automatically based on the attributes of design, but you can set it manually.

Value

The same matrix as design, possibly with some column names shortened by deleting one of prefixes from the start of the column names.

Column names for columns not corresponding to factors are left unchanged.

Note that this is only guaranteed to work correctly if none of your variable names are prefixes of other variable names.

A warning will be issued if, after stripping factor names, the column names of design are no longer unique.

Examples


targets <- data.frame(group = letters[1:2], batch = LETTERS[1:5], x = rnorm(10))
design <- model.matrix(~0 + group + x + batch, targets)
colnames(design)
design2 <- strip_design_factor_names(design)
colnames(design2)


DarwinAwardWinner/rctutils documentation built on July 22, 2022, 5:19 a.m.