简体   繁体   English

在 R 中重新缩放,固定为百分位数

[英]Rescaling in R, fixed to percentile

I understand how to use rescale, but I'm unsure how I could modify it so rather than scaling the min and max (to a 0-100) scale, it scales the min to 0 and x percentile to 100.我了解如何使用重新缩放,但我不确定如何修改它,而不是缩放最小值和最大值(到 0-100)比例,它将最小值缩放到 0 并将 x 百分位数缩放到 100。

Example:例子:

Raw       0-100 scaling (wrong)   0-100 scaling (right [using 75th perecntile as max])
0         0                       0
1         10                      50
2         20                      100
10        100                     500

Current code is:当前代码是:

data$Predict_Party <- rescale(data$Predict_Party_Unscaled, to = c(0, 100))

Additional: How would you recommend I then determine the formula of the transformation that was used?附加:你会建议我如何确定所使用的转换公式? Ie scaled = unscaled*x + y即缩放=未缩放*x + y

Per helpful comment on the choice of algorithm I believe this is the answer.根据对算法选择的有用评论,我相信这就是答案。

library(scales)
raw <- c(0, 1, 2, NA, 10)
rescale(raw, to = c(0,100), from = c(0, quantile(na.omit(raw), .75, type = 1)))
#> [1]   0  50 100  NA 500
df <- data.frame(raw)
df$rescaled <- rescale(df$raw, to = c(0,100), from = c(0, quantile(na.omit(df$raw), .75, type = 1)))
df
#>   raw rescaled
#> 1   0        0
#> 2   1       50
#> 3   2      100
#> 4  NA       NA
#> 5  10      500

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM