//Centering Variables and Generating Z-Scores with Scale() Example //Created by John M. Quick //http://www.johnmquick.com //February 24, 2012 > #read the dataset into an R variable using the read.csv(file) function > dataScale <- read.csv("dataset_scale.csv") > #display the data > dataScale A 1 1 2 1 3 2 4 2 5 4 6 4 7 5 8 5 9 5 10 5 11 6 12 8 13 8 14 8 15 9 16 9 17 9 18 9 19 10 20 10 > #center variable A using the scale() function > scale(dataScale$A, center = TRUE, scale = FALSE) [,1] [1,] -5 [2,] -5 [3,] -4 [4,] -4 [5,] -2 [6,] -2 [7,] -1 [8,] -1 [9,] -1 [10,] -1 [11,] 0 [12,] 2 [13,] 2 [14,] 2 [15,] 3 [16,] 3 [17,] 3 [18,] 3 [19,] 4 [20,] 4 attr(,"scaled:center") [1] 6 > #center variable A manually to verify the results > dataScale$A - mean(dataScale$A) [1] -5 -5 -4 -4 -2 -2 -1 -1 -1 -1 0 2 2 2 3 3 3 3 4 4 > #generate z-scores for variable A using the scale() function< > scale(dataScale$A, center = TRUE, scale = TRUE) [,1] [1,] -1.6522364 [2,] -1.6522364 [3,] -1.3217891 [4,] -1.3217891 [5,] -0.6608946 [6,] -0.6608946 [7,] -0.3304473 [8,] -0.3304473 [9,] -0.3304473 [10,] -0.3304473 [11,] 0.0000000 [12,] 0.6608946 [13,] 0.6608946 [14,] 0.6608946 [15,] 0.9913418 [16,] 0.9913418 [17,] 0.9913418 [18,] 0.9913418 [19,] 1.3217891 [20,] 1.3217891 attr(,"scaled:center") [1] 6 attr(,"scaled:scale") [1] 3.026201 > #generate z-scores for variable A manually to verify the results > (dataScale$A - mean(dataScale$A))/sd(dataScale$A) [1] -1.6522364 -1.6522364 -1.3217891 -1.3217891 -0.6608946 -0.6608946 [7] -0.3304473 -0.3304473 -0.3304473 -0.3304473 0.0000000 0.6608946 [13] 0.6608946 0.6608946 0.9913418 0.9913418 0.9913418 0.9913418 [19] 1.3217891 1.3217891