This function scales a numeric vector to a range between 0 and 1. Primarily intended for processing output from Pseudorandom Number Generators (PRNGs), this scaling transforms values into a standardized range, facilitating downstream analysis and comparative studies.

scale_to_01(x)

Arguments

x

A numeric vector containing values from a PRNG or other numeric data.

Value

A numeric vector where each element is rescaled to fall within the range [0, 1]. If all values in `x` are identical, the function returns a vector of zeros of the same length.

Details

The formula used for rescaling each element \(x_i\) in the vector \(x\) is: $$x_{scaled} = \frac{x_i - \text{min}(x)}{\text{max}(x) - \text{min}(x)}$$

This method of normalization ensures values are linearly scaled, and is commonly used in probabilistic modeling, machine learning, and simulations to transform data into a uniform range [0, 1]. Note that the method may produce NaN values if `x` is a constant vector, which is handled as a special case.

References

- StackOverflow. (2011). Scale a series between two points. Retrieved from https://stackoverflow.com/questions/5468280/scale-a-series-between-two-points - Wikipedia contributors. (2023). Feature scaling. In *Wikipedia, The Free Encyclopedia*. Retrieved from https://en.wikipedia.org/wiki/Feature_scaling

Examples

# Example with PRNG values
set.seed(42)
prng_values <- runif(10, min = -5, max = 5)
scaled_values <- scale_to_01(prng_values)
print(scaled_values)
#>  [1] 0.9722469 1.0000000 0.1887728 0.8671154 0.6319459 0.4790941 0.7501434
#>  [8] 0.0000000 0.6509471 0.7108573