When I look at the source of R Packages, i see the function sweep used quite often.
Sometimes it\'s used when a simpler function would have sufficed (e.g.,
You could use sweep function to scale and center data like the following code. Note that means and sds are arbitrary here (you may have some reference values that you want to standardize data based on them):
df=matrix(sample.int(150, size = 100, replace = FALSE),5,5)
df_means=t(apply(df,2,mean))
df_sds=t(apply(df,2,sd))
df_T=sweep(sweep(df,2,df_means,"-"),2,df_sds,"/")*10+50
This code convert raw scores to T scores (with mean=50 and sd=10):
> df
[,1] [,2] [,3] [,4] [,5]
[1,] 109 8 89 69 15
[2,] 85 13 25 150 26
[3,] 30 79 48 1 125
[4,] 56 74 23 140 100
[5,] 136 110 112 12 43
> df_T
[,1] [,2] [,3] [,4] [,5]
[1,] 56.15561 39.03218 57.46965 49.22319 40.28305
[2,] 50.42946 40.15594 41.31905 60.87539 42.56695
[3,] 37.30704 54.98946 47.12317 39.44109 63.12203
[4,] 43.51037 53.86571 40.81435 59.43685 57.93136
[5,] 62.59752 61.95672 63.27377 41.02349 46.09661