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:
- Creation of Example Data
- Example 1: Extract Variables with select Function
- Example 2: Change Variable Name with rename Function
- Video, Further Resources & Summary
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:
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, 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:
- How to Rename a Column Name in R
- setNames vs. setnames Function in R
- Extract Certain Columns of Data Frame in R
- dplyr R Package
- R Functions List (+ Examples)
- The R Programming Language
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.
Statistics Globe Newsletter