loading...

Center or scale all variables in a data frame. This takes a data frame and return an object of the same class.

# S3 method for data.frame
scale(x, center = TRUE, scale = TRUE)

# S3 method for tbl_df
scale(x, center = TRUE, scale = TRUE)

# S3 method for data.table
scale(x, center = TRUE, scale = TRUE)

Arguments

x

A data frame

center

Are the columns centered (mean = 0)?

scale

Are the column scaled (standard deviation = 1)?

Value

An object of the same class as x.

Examples

data(trees, package = "datasets")
colMeans(trees)
#>    Girth   Height   Volume 
#> 13.24839 76.00000 30.17097 
trees2 <- scale(trees)
head(trees2)
#>        Girth     Height     Volume
#> 1 -1.5768542 -0.9416472 -1.2088547
#> 2 -1.4812561 -1.7263533 -1.2088547
#> 3 -1.4175241 -2.0402357 -1.2149382
#> 4 -0.8758017 -0.6277648 -0.8377598
#> 5 -0.8120696  0.7847060 -0.6917553
#> 6 -0.7802036  1.0985884 -0.6370036
class(trees2)
#> [1] "data.frame"
colMeans(trees2)
#>         Girth        Height        Volume 
#>  1.309436e-16 -7.162729e-18 -8.908644e-17