vanilli
vanilli

Reputation: 131

Sorting a vector with predetermined order

I have a vector 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!

Upvotes: 4

Views: 3633

Answers (1)

Andrie
Andrie

Reputation: 179578

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

Upvotes: 6

Related Questions