Rom*_*ois 16
这样做会.
fun <- function(x){
quantiles <- quantile( x, c(.05, .95 ) )
x[ x < quantiles[1] ] <- quantiles[1]
x[ x > quantiles[2] ] <- quantiles[2]
x
}
fun( yourdata )
Run Code Online (Sandbox Code Playgroud)
您可以使用squish()以下代码在一行代码中执行以下操作:
d2 <- squish(d, quantile(d, c(.05, .95)))
Run Code Online (Sandbox Code Playgroud)
在比例库中,查看?squish和?discard
#--------------------------------
library(scales)
pr <- .95
q <- quantile(d, c(1-pr, pr))
d2 <- squish(d, q)
#---------------------------------
# Note: depending on your needs, you may want to round off the quantile, ie:
q <- round(quantile(d, c(1-pr, pr)))
Run Code Online (Sandbox Code Playgroud)
例:
d <- 1:20
d
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
d2 <- squish(d, round(quantile(d, c(.05, .95))))
d2
# [1] 2 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 19
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20929 次 |
| 最近记录: |