# Order Vector According to Other Vector in R (Example)

In this article, I’ll show you how to order a vector or array according to the elements of another vector in the R programming language.

## Example Vectors

vector_1 <- c(4, 3, 1, 5, 1, 2, 3, 2, 2, 1, 4, 5) # Create first vector vector_2 <- c(5, 4, 1, 2, 3) # Create second vector |

vector_1 <- c(4, 3, 1, 5, 1, 2, 3, 2, 2, 1, 4, 5) # Create first vector vector_2 <- c(5, 4, 1, 2, 3) # Create second vector

## Reorder First Vector Based on Second Vector

We can apply the match and order functions to rearrange our first vector according to the second vector:

vector_1[order(match(vector_1, vector_2))] # Ordering vector_1 # 5 5 4 4 1 1 1 2 2 2 3 3 |

vector_1[order(match(vector_1, vector_2))] # Ordering vector_1 # 5 5 4 4 1 1 1 2 2 2 3 3