# Basic usage with matrix
set.seed(123)
mat <- matrix(rnorm(30, mean = 5:7, sd = 1:3), ncol = 3,
dimnames = list(paste0("Obs", 1:10), paste0("Var", 1:3)))
norm_mat <- normalize(mat)
# Verify attributes
attr(norm_mat, "scaled:center") # Original column means
attr(norm_mat, "scaled:scale") # Original column standard deviations
# Verify properties
apply(norm_mat, 2, mean) # Should be near zero
apply(norm_mat, 2, sd) # Should be exactly 1
# With data frame input
df <- as.data.frame(mat)
norm_df <- normalize(df)
all.equal(norm_mat, norm_df, check.attributes = FALSE) # Should be identical
# Handling constant columns (produces NaN)
const_mat <- cbind(mat, Constant = rep(4.2, 10))
normalize(const_mat)
Run the code above in your browser using DataLab