Order Data Frame Rows According to Vector in R (2 Examples)
In this R tutorial you’ll learn how to sort data frame rows based on the values of a vector with a specific order.
Table of contents:
It’s time to dive into the examples:
Creation of Example Data
Let’s first create some example data.
data <- data.frame(x1 = 11:15, # Create example data x2 = letters[1:5], x3 = 6:10) data # Print example data # x1 x2 x3 # 1 11 a 6 # 2 12 b 7 # 3 13 c 8 # 4 14 d 9 # 5 15 e 10
As you can see based on the previous output of the RStudio console, the example data has five rows and three columns.
We also have to create a vector with specific ordering:
vec <- c("b", "e", "a", "c", "d") # Create example vector vec # Print example vector # "b" "e" "a" "c" "d"
Our example vector contains the same values as the variable x2, but with different order.
Example 1: Sorting Data Frame According to Vector Using match() Function
In Example 1, I’ll show how to reorder the rows of a data frame based on the values of a custom vector using the match function provided by the basic installation of the R programming language.
Consider the following R code:
data_new1 <- data[match(vec, data$x2), ] # Reorder data frame data_new1 # Print updated data frame # x1 x2 x3 # 2 12 b 7 # 5 15 e 10 # 1 11 a 6 # 3 13 c 8 # 4 14 d 9
As you can see based on the previous output of the RStudio console, we have created a new data table with rearranged rows. The rows of this new data frame were sorted according to the values of our vector.
Example 2: Sorting Data Frame According to Vector Using left_join() Function of dplyr Package
Alternatively to the R code of Example 1, we can also use the functions of the dplyr package to sort our data frame according to a vector.
First, we have to install and load the dplyr package:
install.packages("dplyr") # Install & load dplyr package library("dplyr")
Now, we can use the left_join function to order our data frame rows according to the values in our vector:
data_new2 <- left_join(data.frame(x2 = vec), # Reorder data frame data, by = "x2") data_new2 # Print updated data frame # x2 x1 x3 # 1 b 12 7 # 2 e 15 10 # 3 a 11 6 # 4 c 13 8 # 5 d 14 9
The previous output is exactly the same as in Example 1, but this time we reordered our data frame using the dplyr package.
Video, Further Resources & Summary
If you need more information on the R syntax of this article, you may have a look at the following video that I have published on my YouTube channel. In the video, I show the examples of this tutorial.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may want to read the other articles of my website. You can find some tutorials about similar topics such as extracting data, the row names attribute, vectors, and character strings below:
- Conditionally Remove Row from Data Frame
- sort, order & rank R Functions
- Select Data Frame Column Using Character Vector
- Repeat Rows of Data Frame N Times
- Select Data Frame Rows based on Values in Vector
- R Programming Tutorials
Summary: In this R tutorial you learned how to rearrange data matrix rows according to a vector (or array). Don’t hesitate to let me know in the comments section below, in case you have any further questions and/or comments.
Statistics Globe Newsletter