How do I sort one vector based on values of another Ask Question

How do I sort one vector based on values of another Ask Question

I have a vector x, that I would like to sort based on the order of values in vector y. The two vectors are not of the same length.

x <- c(2, 2, 3, 4, 1, 4, 4, 3, 3)
y <- c(4, 2, 1, 3)

The expected result would be:

[1] 4 4 4 2 2 1 3 3 3

ベストアンサー1

what about this one

x[order(match(x,y))]

おすすめ記事