Rescaling Data using R

reshape package provides with convenient function called rescaler:

> d = data.frame(x=1:5)
> library(reshape)
> rescaler(d$x, type="sd")
[1] -1.2649111 -0.6324555  0.0000000  0.6324555  1.2649111
> with(d, (1-mean(x))/sd(x))
[1] -1.264911

There are other types of rescaling: range(to make data 0~1), rank(rank data by value), robust(use median and MAD instead of mean and sd).

> rescaler(d$x, type="robust")
[1] -1.3489815 -0.6744908  0.0000000  0.6744908  1.3489815

But just to remove mean without dividing data with any number, use scale:

> scale(d$x, scale=F)
     [,1]
[1,]   -2
[2,]   -1
[3,]    0
[4,]    1
[5,]    2
attr(,"scaled:center")
[1] 3

Similar Posts:

Post a Comment

Your email is never published nor shared. Required fields are marked *