开发者

Sorting a vector with predetermined order

I have a v开发者_如何学Cector x of length 10 that I would like to sort based on the order of values in vector y (1:10). Say:

x <- c(188,43,56,3,67,89,12,33,123,345)
y <- c(3,4,5,7,6,9,8,2,1,10) 

The vector y will always consist of numbers from 1 to 10, but in different orders. I'd like to match the lowest value in x with 1 and the highest value with 10 so that the output will be something like

x_new <-(33,43,56,67,89,123,188,12,3,345)

How can I do this? I appreciate any input!


sort(x)[y]
 [1]  33  43  56  89  67 188 123  12   3 345
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜