Rescaling a variable in R

I have a variable named Esteem that is at a scale of 1: 7. I would like to rescale it to 1: 100. I understand that R software scales can do this, however I am having syntax problems.

Can anyone provide an example of how I can rescale this variable? Also, is there a tool I can use in R Commander to do this?

Many thanks!

+3


source to share


2 answers


I don't know RCommander. There is a package called RPMG

that has a zoom function that is usually used for graphic purposes. I'm not sure if it completely does what you want (since you didn't provide an example including example output).

But it might be relevant.

set.seed(1)
x<-sample(1:7, 10, replace=T)
x
#[1] 2 3 5 7 2 7 7 5 5 1

library(RPMG)
RESCALE(x, 1, 100, 1, 7)
#[1]  17.5  34.0  67.0 100.0  17.5 100.0 100.0  67.0  67.0   1.0

      



Internally, the RESCALE

arguments after x: new.min, new.max, old.min, old.max scale.

This function is actually very simple:

RESCALE <- function (x, nx1, nx2, minx, maxx) 
{ nx = nx1 + (nx2 - nx1) * (x - minx)/(maxx - minx)
  return(nx)
}

      

+4


source


You can do something like this with the R base too (using @jalapics data)



seq(1, 100, length.out = 7)[x]
## [1]  17.5  34.0  67.0 100.0  17.5 100.0 100.0  67.0  67.0   1.0

      

+2


source







All Articles