Reputation: 531
i got this vector:
set.seed(123)
vector <- sort(sample(0:1000, 500, replace=FALSE))
and this quantile numbers of vector:
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
My question is: how to make each element of the vector be replaced by the closest value of the quantile.
result vector: (something like this)
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......)
Upvotes: 2
Views: 256
Reputation: 388982
To get the closest value of the quantile you can use subtract each number with quantile and select the one which is closest one.
You can implement this using sapply
res <- unname(q[sapply(vector, function(x) which.min(abs(x - q)))])
Or with outer
-
res <- unname(q[apply(abs(outer(vector, q, `-`)), 1, which.min)])
Upvotes: 2
Reputation: 887108
We can use findInterval
as.numeric(q[findInterval(vector, q)])
-output
[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
Upvotes: 2