This should be an easy question... I'm looking to scale a numeric vector to a specified mean and range.
For instance, I would like to scale a vector, x
, to mean = 1, min = 0, and max = 2. Here's my vector x
, with min = 0, max = 1:
> x <- runif(100, 0, 1)
> mean(x)
[1] 0.4897899
My first thought was to scale to a mean = 0, using scale()
, and add 1... But this doesn't address the issue of the range:
> mean(scale(x)+1)
[1] 1
But, this results in negative values:
> sum((scale(x)+1) < 0)
[1] 21
My next thought was to use scales::rescale()
, but it appears to only have arguments for setting the desired range.
This seems like an easy question, but I'm stumped! Any help would be greatly appreciated.
-Alex.
question from:
https://stackoverflow.com/questions/65911413/scale-numeric-vector-to-specified-mean-min-and-max 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…