Sort Variables of Data Frame by Column Names in R (2 Examples)
In this R tutorial you’ll learn how to order variables of a data matrix by column names.
The tutorial will consist of the following topics:
Let’s jump right to the examples!
Creation of Example Data
Let’s first create some example data:
data <- data.frame(a = 1:5, # Create data d = letters[1:5], c = 3, b = 5:1) data # Show data # a d c b # 1 1 a 3 5 # 2 2 b 3 4 # 3 3 c 3 3 # 4 4 d 3 2 # 5 5 e 3 1 |
data <- data.frame(a = 1:5, # Create data d = letters[1:5], c = 3, b = 5:1) data # Show data # a d c b # 1 1 a 3 5 # 2 2 b 3 4 # 3 3 c 3 3 # 4 4 d 3 2 # 5 5 e 3 1
As you can see based on the previous output of the RStudio console, our exemplifying data has five rows and four columns. The variable names of our columns are unordered.
Example 1: Order Data Frame Columns by Variable Names Using order & names Functions
The following R syntax explains how to use the order and names functions to reorder the variables of a data table alphabetically.
data_new1 <- data[ , order(names(data))] # Apply order & names data_new1 # Print updated data # a b c d # 1 1 5 3 a # 2 2 4 3 b # 3 3 3 3 c # 4 4 2 3 d # 5 5 1 3 e |
data_new1 <- data[ , order(names(data))] # Apply order & names data_new1 # Print updated data # a b c d # 1 1 5 3 a # 2 2 4 3 b # 3 3 3 3 c # 4 4 2 3 d # 5 5 1 3 e
As you can see based on the previous output, our data frame columns were reordered in alphabetical order.
Example 2: Order Data Frame Columns by Variable Names Using dplyr Package
In this Example, I’ll illustrate how to use the functions of the dplyr package to sort a data frame in R. We first have to install and load the dplyr package, if we want to use the corresponding functions:
install.packages("dplyr") # Install & load dplyr package library("dplyr") |
install.packages("dplyr") # Install & load dplyr package library("dplyr")
Now, we can apply the following R code to sort our data frame with dplyr:
data_new2 <- data %>% # Sort with dplyr select(sort(names(.))) data_new2 # Print updated data # a b c d # 1 1 5 3 a # 2 2 4 3 b # 3 3 3 3 c # 4 4 2 3 d # 5 5 1 3 e |
data_new2 <- data %>% # Sort with dplyr select(sort(names(.))) data_new2 # Print updated data # a b c d # 1 1 5 3 a # 2 2 4 3 b # 3 3 3 3 c # 4 4 2 3 d # 5 5 1 3 e
The output is exactly the same as in Example 1.
Video & Further Resources
Have a look at the following video of the Statistics Globe YouTube channel. I illustrate the R code of this tutorial in the video.
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.
In addition, I can recommend to have a look at the related posts of my website:
- sort, order & rank R Functions
- Randomly Reorder Data Frame by Row and Column
- Reorder Columns of Data Frame in R
- Insert New Column Between Two Data Frame Variables
- Remove Data Frame Columns by Name
- dplyr Package Tutorial
- The R Programming Language
In this article you learned how to reorder data frame columns by name in R programming. Let me know in the comments, in case you have additional questions.
Statistics Globe Newsletter