standardize: Standardize Columns in a Data Frame or Matrix

View source: R/S05_Statistics.R

standardizeR Documentation

Standardize Columns in a Data Frame or Matrix

Description

Function to standardize (mean-center and scale by standard deviation resulting in a mean of 0 and standard deviation of 1) columns in a matrix or data frame.

Usage

standardize(
  x,
  y = NULL,
  mean_sd = NULL,
  raw = FALSE,
  as_list = FALSE,
  labels = c("X", "Y")
)

Arguments

x

A data frame or matrix of numeric values.

y

A data frame or matrix of numeric values (must have the same column names in same order as x).

mean_sd

A list of two numeric vectors equal in length to the number of columns with the means and standard deviations, respectively, to use for scaling.

raw

Logical; if TRUE, uses the means and standard deviations given in mean_sd to return the original raw values of x.

as_list

Logical; if TRUE returns a named list with the scaled values of x (and y if provided) along with the means and standard deviations used for scaling. Automatically set to TRUE when y is provided.

labels

A character vector with the labels for the x and y data sets if returning a list.

Value

Either a scaled data frame or matrix or a list with the scaled values and the means and standard deviations used for scaling.

Examples

# Create data frame
x_raw <- round( matrix( rnorm( 9, 100, 15 ), 3, 3 ) )
colnames(x_raw) <- paste0( 'X', 1:3 )
print(x_raw)

# Standardize columns
x <- standardize( x_raw )
print(x)

# Create second data frame with same
# variables but new values
y_raw <- round( matrix( rnorm( 9, 50, 15 ), 3, 3 ) )
colnames(y_raw) <- paste0( 'X', 1:3 )
print(y_raw)

# Scale columns of y_raw based on means and
# standard deviations from x_raw
lst <- standardize( x_raw, y_raw, labels = c('x', 'y') )
y <- lst$Data$y
print( y )

# Undo scaling
standardize( y, mean_sd = lst$Scaling, raw = TRUE )


rettopnivek/arfpam documentation built on Oct. 20, 2024, 7:24 p.m.