scale
Scale Columns of a Matrix

### Description

Centers and then scales the columns of a matrix. By default, each column in the result has mean 0 and sample standard deviation 1.
This function is an S Version 3 generic. (See Methods.) Method functions can be written to handle specific S Version 3 classes of data. Currently there is only the default method.

### Usage

    scale(x, center = TRUE, scale = TRUE)
    ## Default S3 method:
    scale(x, center = TRUE, scale = TRUE)

### Arguments

x | the matrix to be scaled. Missing values (NAs) are allowed.
center | specifies whether or how to control the value subtracted from each column. Can be a logical value or a vector. If TRUE (the default), the mean of the non-missing data in each column is subtracted from the column. If given as a vector of length ncol(x), this vector is used. That is, center[j] is subtracted from column j. If FALSE, no centering is done.
scale | specifies whether or how to control the value divided into each column to scale it. Can be a logical value or a vector. If TRUE (the default), after centering, each column is divided by the square root of sum-of-squares over n-1, where n is the number of non-missing values. If given as a vector of length ncol(x), column j is divided by scale[j]. If FALSE, no scaling is done.

### Details

scale takes as an argument x (matrix) and returns a similar matrix whose columns take elements corresponding to the center (mean), and scale (standard deviation). If center and scale are not specified, the default values are 0 and 1, respectively.
If scale or center is not a logical value, and its length is not equal to the number of columns in x, an error is generated.
Value
returns a matrix like x with optional centering and scaling.

- If scale is TRUE or a vector (that is, not FALSE), this matrix has an attribute scaled:scale containing the vector of values used to scale the columns.
- If center is TRUE or a vector (that is, not FALSE), this matrix has an attribute scaled:center containing the vector of values used to center the columns.

References
Becker, R. A., Chambers, J. M., and Wilks, A. R. 1988. The New S Language. Pacific Grove, CA: Wadsworth & Brooks/Cole Advanced Books and Software.
See Also
sweep, apply, var, mad.
Examples

    A <- matrix(c(1:3,3*(1:3),2^(1:3)), nrow=3)
    A
    #      [,1] [,2] [,3]
    # [1,]    1    3    2
    # [2,]    2    6    4
    # [3,]    3    9    8
    # center and scale
    scale(A)
    #      [,1] [,2]       [,3]
    # [1,]   -1   -1 -0.8728716
    # [2,]    0    0 -0.2182179
    # [3,]    1    1  1.0910895
    # attr(,"scaled:center")
    # [1] 2.000000 6.000000 4.666667
    # attr(,"scaled:scale")
    # [1] 1.00000 3.00000 3.05505
    # check column means and variances of centered and scaled matrix
    apply(scale(A),2,mean)
    # [1]  0.000000e+000  0.000000e+000 -1.480297e-016
    apply(scale(A),2,var)
    # [1] 1 1 1
    # just center
    scale(A, scale=FALSE)
    #      [,1] [,2]       [,3]
    # [1,]   -1   -3 -2.6666667
    # [2,]    0    0 -0.6666667
    # [3,]    1    3  3.3333333
    # attr(,"scaled:center")
    # [1] 2.000000 6.000000 4.666667
    # just scale
    scale(A, center=FALSE)
    #           [,1]      [,2]      [,3]
    # [1,] 0.3779645 0.3779645 0.3086067
    # [2,] 0.7559289 0.7559289 0.6172134
    # [3,] 1.1338934 1.1338934 1.2344268
    # attr(,"scaled:scale")
    # [1] 2.645751 7.937254 6.480741
    # just center by a vector of values
    scale(A, center=c(1,2,4), scale=FALSE)
    #      [,1] [,2] [,3]
    # [1,]    0    1   -2
    # [2,]    1    4    0
    # [3,]    2    7    4
    # attr(,"scaled:center")
    # [1] 1 2 4
    # just scale by a vector of values
    scale(A, center=FALSE, scale=c(1,2,4))
    #      [,1] [,2] [,3]
    # [1,]    1  1.5  0.5
    # [2,]    2  3.0  1.0
    # [3,]    3  4.5  2.0
    # attr(,"scaled:scale")
    # [1] 1 2 4

Package base version 6.1.5-18
Package Index