The scale function
For a numeric matrix, you might want to scale the values within a column so that they have a mean of 0. You might also want to know the standard deviation of the values within each column. These two actions are carried out simultaneously with the scale function:
scale(counts)
[,1] [,2] [,3]
[1,] −0.7833495 −0.439155 −1.224745
[2,] −0.7833495 1.317465 1.224745
[3,] 1.3055824 0.146385 0.000000
[4,] 0.2611165 −1.024695 0.000000
attr(,"scaled:center")
[1] 2.75 1.75 3.00
attr(,"scaled:scale") [1] 0.9574271 1.7078251 1.6329932
The values in the table are the counts minus the column means of the counts. The means of the columns – attr(,"scaled:center") – are 2.75, 1.75 and 3.0, while the standard deviations of the columns – attr(,"scaled:scale") – are 0.96, 1.71 and 1.63. To check that the scales are the standard deviations (sd) of the counts within a column, you could use apply to the columns (margin = 2) like this:
apply(counts,2,sd)
[1] 0.9574271 1.7078251 1.6329932
Get The R Book now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.