How do I scale a series such that the first number in the series is 0 and last number is 1. I looked into 'approx', 'scale' but they do not achieve this objective.
# generate series from exponential distr
s = sort(rexp(100))
# scale/interpolate 's' such that it starts at 0 and ends at 1?
# approx(s)
# scale(s)
This should do it:
EDIT
I was curious about the performance of the two methods
Extracting the raw code from
reshape::rescaler.default
Yields similar result.
Alternatively:
(untested)
The
scales
package has a function that will do this for you:rescale
.By default, this scales the given range of
s
onto 0 to 1, but either or both of those can be adjusted. For example, if you wanted it scaled from 0 to 10,or if you wanted the largest value of
s
scaled to 1, but 0 (instead of the smallest value ofs
) scaled to 0, you could useIt's straight-forward to create a small function to do this using basic arithmetic:
You can also make use of the caret package which will provide you the preProcess function which is just simple like this:
More details on the package help.