R dplyr & plyr Error: Can’t rename columns that don’t exist. (2 Examples)

 

On this page you’ll learn how to reproduce and debug the “Error: Can’t rename columns that don’t exist.” in the R programming language.

The tutorial contains two examples for the debugging of error messages. To be more precise, the article will contain this:

Let’s start right away.

 

Creation of Example Data

We’ll use the following data as basement for this R programming tutorial:

data <- data.frame(x1 = 5:1,                       # Create example data frame
                   x2 = letters[1:5])
data                                               # Print example data frame

 

table 1 data frame r error cant rename columns that dont exist

 

Table 1 shows the structure of our example data – It consists of five rows and two columns. The variables of our data frame are called x1 and x2.

 

Example 1: Reproduce the Error: Can’t rename columns that don’t exist.

In this example, I’ll illustrate how to replicate the “Error: Can’t rename columns that don’t exist.” in R.

For this, we first have to install and load the plyr package

install.packages("plyr")                           # Install plyr package
library("plyr")                                    # Load plyr

…and afterwards the dplyr package:

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

Now, let’s assume that we want to rename the column names of our data frame using the rename function. Then, we might try to run the following R code:

data_new <- rename(data, c("x1" = "col1",          # Try to rename columns
                           "x2" = "col2"))
# Error: Can't rename columns that don't exist.
# x Column `col1` doesn't exist.
# Run `rlang::last_error()` to see where the error occurred.

Unfortunately, the RStudio console returns the “Error: Can’t rename columns that don’t exist.”.

The reason for this is that both plyr and dplyr contain a function called rename. Since we have loaded the dplyr package last, the R programming language tries to use the rename function of the dplyr package.

However, the previous R code is designed to use the rename function of the plyr package and, hence, returns the error message.

In the following example, I’ll explain how to avoid this problem. So keep on reading!

 

Example 2: Fix the Error: Can’t rename columns that don’t exist.

This example explains how to solve the problems with the rename function, i.e. that R uses the rename function of the wrong package.

To make sure that we use the function of the plyr package, we have to specify the name of the package in front of the function as shown below:

data_new <- plyr::rename(data, c("x1" = "col1",    # rename() function of plyr
                                 "x2" = "col2"))
data_new                                           # Print renamed data

 

table 2 data frame r error cant rename columns that dont exist

 

In Table 2 it is shown that we have created a new data frame with renamed variable names. The RStudio console did not return any error messages anymore.

 

Video, Further Resources & Summary

Have a look at the following video that I have published on my YouTube channel. In the video, I illustrate the R codes of this article.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at the related tutorials on my homepage:

 

In summary: In this tutorial you have learned how to handle the “Error: Can’t rename columns that don’t exist.” in the R programming language.

Note that depending on the R, dplyr, and plyr versions that you have installed the error message might be shown a bit differently. For example, in some settings the error message “Error: All arguments to rename must be named.” might appear.

Please tell me about it in the comments below, in case you have any additional questions. Furthermore, don’t forget to subscribe to my email newsletter to get regular updates on the newest articles.

 

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.


10 Comments. Leave new

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.

Top