View source: R/S05_Statistics.R
standardize | R Documentation |
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.
standardize(
x,
y = NULL,
mean_sd = NULL,
raw = FALSE,
as_list = FALSE,
labels = c("X", "Y")
)
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 |
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 |
as_list |
Logical; if |
labels |
A character vector with the labels for
the |
Either a scaled data frame or matrix or a list with the scaled values and the means and standard deviations used for scaling.
# 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 )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.