I am trying to rename a column with dplyr::rename()
and R is returning this error that I am unable to find anywhere online.
Error: `new_name` = old_name must be a symbol or a string, not formula
Reproducible example with a 2 column data frame:
library(dplyr)
df <- data.frame(old_name = seq(1:10), x = seq(1:10))
df %>% dplyr::rename(new_name = old_name)
Session info:
> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin17.2.0 (64-bit)
Running under: macOS High Sierra 10.13.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_0.7.4
loaded via a namespace (and not attached):
[1] compiler_3.4.3 magrittr_1.5 assertthat_0.2.0 R6_2.2.2
[5] bindrcpp_0.2 glue_1.2.0 tibble_1.3.4 Rcpp_0.12.14.3
[9] pkgconfig_2.0.1 rlang_0.1.4.9000 bindr_0.1
>
I expect this new simple data frame to have the first column renamed to new_name
. This also does not work with rename_()
.
Current R version is 3.4.3 and dplyr version is 0.7.4. I was unable to replicate this on R version 3.3.3, but was able to replicate it on R version 3.4.0. This was tested on a completely clean R session.
My current solution is to rewrite part of my code with plyr::rename
as that still works, but this is not ideal because it requires me to rewrite a lot of code.
Working example with plyr()
:
library(plyr)
df <- data.frame(old_name = seq(1:10), x = seq(1:10))
df %>% plyr::rename(replace = c('old_name' = 'new_name'))
rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to change the column from old to a new name. The following example renames the column from id to c1 . The operator – %>% is used to load the renamed column names to the data frame.
Unfortunately, the “Error: Can't rename columns that don't exist.” message appears in the RStudio console. The reason for this is that both plyr and dplyr provide a rename function. Because we loaded the dplyr package last, the R programming language tries to use the dplyr package's rename function.
As @aosmith commented, this is a result of using the dev version of the rlang package (from GitHub) with the released version of dplyr (from CRAN). Full discussion is here: https://github.com/tidyverse/dplyr/issues/3252
Both packages should be from CRAN or both from GitHub; the mismatch is the problem. To fix this, you can update your dplyr to the dev version with devtools::install_github("tidyverse/dplyr")
or revert your rlang install back to the current CRAN version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With