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 |
data <- data.frame(x1 = 5:1, # Create example data frame x2 = letters[1:5]) data # Print example data frame
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 |
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 |
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. |
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 |
data_new <- plyr::rename(data, c("x1" = "col1", # rename() function of plyr "x2" = "col2")) data_new # Print renamed data
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.
10 Comments. Leave new
This SAVED my thesis! Thanks!
Haha, that’s great to hear Lorraine! Good luck with finishing your thesis 🙂
Joachim
I spent three hours, until this blog saved me
Nice to hear that it helped! 🙂
Regards
Joachim
Thank you so much! I was in the panic mode because my previous code does not work! Thank you so much for pointing this out and providing such a nice solution. I would scratch my head for days to figure out what’s going on!
Hey Ting,
This is great to hear! Glad it was helpful to you!
Regards,
Joachim
I wasted so much time trying to solve this.. Thanks for this write up
Thanks for the kind comment Frazer, glad it was helpful!
Thank you so much, at the same time only run one library (dplyr) or library(plyr), plyr is good one.
Hey,
Thank you for the kind comment, glad it was helpful! 🙂
Regards,
Joachim