How to Rescale Value to a Specific Range?

I'm trying to rescale a dataset of Rdata comprised between 0.17 and 0.00002589 to a range between 0 and 1. I would like the cumulative frequence to be equal to 1.

I tried to use the function rescale (from scales package) :

library(scales)

t <-rescale(df, to = c(0,1)) #data go from [0.00002589:0.17] to [0.53:0.00004914]

cumsum(t)= 4.24...

How do I correct my data in order to have cumsum equal to 1 ?

Thank you for your help.

1 Answer

You could add

t <- t / sum(t)

For example:

set.seed(42)
df <- rnorm(n = 10, 1)
t <- scales::rescale(df, to = c(0,1))
t <- t / sum(t)
cumsum(t)


#[1] 0.1740706 0.1740706 0.2575087 0.3652035 0.4523411 0.4935800
# [7] 0.6802913 0.7225612 0.9548573 1.0000000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest