Ramon
Ramon

Reputation: 31

Rescaling a variable in R

I have a variable named Esteem that is in a scale 1:7. I would like to rescale it to 1:100. I understand that the R program scales can do so, however I am having problems with the syntax.

Could someone provide an example of how can I rescale that variable? Also, is there a tool that I can use in R Commander to do that?

Thanks very much!

Upvotes: 3

Views: 9994

Answers (2)

jalapic
jalapic

Reputation: 14192

I don't know RCommander. There is a package called RPMG that has a rescaling function, which is generally used for graphic purposes. I'm not sure it's entirely doing what you want (as you haven't provided an example including example output).

But, this may 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

Within RESCALE the arguments after x are: the new.min, new.max, old.min, old.max of a scale.

This function is actually very simple:

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

Upvotes: 5

David Arenburg
David Arenburg

Reputation: 92282

You can do something like that using base R 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

Upvotes: 3

Related Questions