24 Scale

scale, with default settings, will calculate the mean and standard deviation of the entire vector, then “scale” each element by those values by subtracting the mean and dividing by the sd. (If you use scale(x, scale=FALSE), it will only subtract the mean but not divide by the std deviation.)

   set.seed(1)
   x <- runif(7)

   # Manually scaling
   (x - mean(x)) / sd(x)
## [1] -1.01951259 -0.68940037 -0.06788275  0.97047346 -1.21713898  0.94007371
## [7]  1.08338753
   scale(x)
##             [,1]
## [1,] -1.01951259
## [2,] -0.68940037
## [3,] -0.06788275
## [4,]  0.97047346
## [5,] -1.21713898
## [6,]  0.94007371
## [7,]  1.08338753
## attr(,"scaled:center")
## [1] 0.5947772
## attr(,"scaled:scale")
## [1] 0.3229666

The Scale() Function The scale() function makes use of the following arguments.

x: a numeric object center: if TRUE, the objects’ column means are subtracted from the values in those columns (ignoring NAs); if FALSE, centering is not performed scale: if TRUE, the centered column values are divided by the column’s standard deviation (when center is also TRUE; otherwise, the root mean square is used); if FALSE, scaling is not performed Centering Variables Normally, to center a variable, you would subtract the mean of all data points from each individual data point. With scale(), this can be accomplished in one simple call.

scale() function centers and/or scales the columns of a numeric matrix.

Normally, to create z-scores (standardized scores) from a variable, you would subtract the mean of all data points from each individual data point, then divide those points by the standard deviation of all points. Again, this can be accomplished in one call using scale().

#generate z-scores for variable A using the scale() function scale(A, center = TRUE, scale = TRUE)

scale(x, center = TRUE, scale = TRUE)

x: numeric matrix center: either a logical value or a numeric vector of length equal to the number of columns of x scale: either a logical value or a numeric vector of length equal to the number of columns of x

x <- matrix(1:9,3,3)
scale(x)
##      [,1] [,2] [,3]
## [1,]   -1   -1   -1
## [2,]    0    0    0
## [3,]    1    1    1
## attr(,"scaled:center")
## [1] 2 5 8
## attr(,"scaled:scale")
## [1] 1 1 1
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9