A variation over the base R scale function that avoids the "divide by 0 = NA" problem.
A variation over the base R scale function that avoids the "divide by 0 = NA" problem.
scale1: A variation over the base Rscale function. The function scale1: centers (if needed) and scales a vector to norm 1; if the vector contains values all equal to a constant, scale1 sets all values to 0 (in lieu of NA as scale does). Usefull when pre-processing tables for PCA-like type of analyses.
scale1(x, scale =TRUE, center =TRUE)
Arguments
x: a vector to be scaled
scale: (default = TRUE), when TRUE
scale the vector to norm 1, otherwise do nothing.
center: (default = TRUE), when TRUE
center the vectors (i.e., substract the mean from all numbers), otherwise do nothing.
Returns
a centered (if required) and norm-1 (if required) normalized vector.
Examples
toto <- runif(10)# 10 random numbers between 0 and 1 tutu <- scale1(toto)# toto centered and normalized toto0 <- rep(1,10)# 10 numbers all equal to 1 tutu0 <- scale1(toto0)# scaled to 0 # Compare with tutuNA <- scale(toto0)# all numbers set to NA