[R] Changing data scales

Joshua Wiley jwiley.psych at gmail.com
Mon Aug 22 04:31:40 CEST 2011


On Sun, Aug 21, 2011 at 5:50 PM, Jorge I Velez <jorgeivanvelez at gmail.com> wrote:
> On 8/21/11 8:04 PM, Jim Silverton wrote:
>>
>> I have data that ranges from 0.3 to 2 and I want to change the scale to be
>> from 0 to 1.
>> Can this be done in R?
>>
> Take a look at ?scale

Which leads one to:

scale(x, .3, 1.7)

but this is far from the only possible way.  Another possibility would be:

f <- function(x) ifelse(x < 0, (1/(2 - x)), (1 - 1/(2 + x)))

finv <- function(y) ifelse((0 < y) & (y < .5), (2 - (1/y)), (1/(1-y) - 2))

## use function to move to the (0, 1) set
f(seq(.3, 2, .1))

## show the inverse works
finv(f(seq(.3, 2, .1)))

## Graph the two functions
curve(f, from = -100, to = 100, n = 1000)
curve(finv, from = .001, to = .999, n = 1000)

although this technically puts you into the (0, 1) set rather than [0,
1].  For more information on this general sort of thing, search for
"bijection".  By the way, I believe I owe credit for those functions
to one of my math texts at some point, but I cannot remember the
reference (or perhaps my younger self was smarter than I recall (or my
memory has gotten worse than I think...)).

Josh


>
> HTH,
> Jorge
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list