Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

r - Scale numeric vector to specified mean, min, and max

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...