gpt4 book ai didi

r - 根据分位数的接近程度更改向量的值

转载 作者:行者123 更新时间:2023-12-04 07:35:30 24 4
gpt4 key购买 nike

我得到了这个向量:

set.seed(123)
vector <- sort(sample(0:1000, 500, replace=FALSE))
和这个向量的分位数:
q <- quantile(vector, probs = seq(0,1,0.1))

> 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
> 4.0 102.6 191.6 292.4 390.6 480.5 598.2 698.6 796.6 900.1 1000.0
我的问题是:如何使向量的每个元素都被最接近的分位数值替换。
结果向量:(类似这样)
result = c(4,4,4,4,4,4,4,4,4,4,4,4,4......102.6,102.6,102.6,102.6,102.6,102.6,102.6,.....191.6......)

最佳答案

我们可以使用 findInterval

as.numeric(q[findInterval(vector, q)])
-输出
 [1]    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0    4.0
[22] 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0
[43] 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6
[64] 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6
[85] 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 102.6 191.6 191.6 191.6 191.6 191.6
[106] 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6
[127] 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6 191.6
[148] 191.6 191.6 191.6 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4
[169] 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4
[190] 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 292.4 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6
[211] 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6
[232] 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 390.6 480.5 480.5
[253] 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5
[274] 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5 480.5
[295] 480.5 480.5 480.5 480.5 480.5 480.5 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2
[316] 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2
[337] 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 598.2 698.6 698.6 698.6 698.6 698.6 698.6 698.6
[358] 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6
[379] 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6 698.6
[400] 698.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6
[421] 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6
[442] 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 796.6 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1
[463] 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1
[484] 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 900.1 1000.0

关于r - 根据分位数的接近程度更改向量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67766337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com