select & rename R Functions of dplyr Package (2 Examples)


In this R tutorial you’ll learn how to select and rename variables with the select() and rename() functions of the dplyr package.

The tutorial consists of two examples for the selection and renaming of variables in R. To be more specific, the content of the article looks as follows:

Let’s just jump right in.


Creation of Example Data

We will use the following data frame for the examples of this R programming tutorial:

data <- data.frame(x1 = 1:3,                    # Create example data
                   x2 = LETTERS[1:3],
                   x3 = 5)
data                                            # Print example data
#   x1 x2 x3
# 1  1  A  5
# 2  2  B  5
# 3  3  C  5

Our data frame contains three rows and three columns. Note that we could also apply the following R code to a tibble of the tidyverse instead of a data frame.

Furthermore, we need to install and load the dplyr R package:

install.packages("dplyr")                       # Install dplyr
library("dplyr")                                # Load dplyr


Example 1: Extract Variables with select Function

Example 1 explains how to apply the select function of the dplyr package. Let’s assume that we want to extract the variables x1 and x3 of our data frame. Then, we can use the select function as shown below:

select(data, c(x1, x3))                         # Apply select function
#   x1 x3
# 1  1  5
# 2  2  5
# 3  3  5

As you can see based on the previous output of the RStudio console, the select function returned a subset of our original data frame containing only the two selected columns.


Example 2: Change Variable Name with rename Function

With the rename function, we can change the column names of certain variables.

Note that it is important to write dplyr:: in front of the rename function. Several R packages contain a rename function and with dplyr:: we tell R to use the rename function of the dplyr package.

However, have a look at the following R code:

dplyr::rename(data, x1_new = x1)                # Apply rename function
#   x1_new x2 x3
# 1      1  A  5
# 2      2  B  5
# 3      3  C  5

We changed the name of the first column of our data frame from x1 to x1_new.


Video, Further Resources & Summary

Do you need further info on the R syntax of this tutorial? Then you might have a look at the following video instruction of my YouTube channel. I’m explaining the contents of this tutorial in the video:



In addition, you may have a look at some of the related articles on this website. I have released numerous articles on the handling of data frames in R already:


In this R article you learned how to extract and change the name of data frame and tibble columns with the dplyr package. Tell me about it in the comments below, in case you have further questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.