Reputation: 191
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'))
Upvotes: 19
Views: 4365
Reputation: 23014
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.
Upvotes: 16
Reputation: 1648
I had the same problem. After updating all packages just in case, it works (see sessionInfo()
below.
Switch rename
to select
(which was working for some reason)
df <- data.frame(old_name = seq(1:10), x = seq(1:10))
## df %>% dplyr::rename(new_name = old_name) # error
df %>% dplyr::select(new_name = old_name, everything())
That might be easier than the plyr
strat, and if updating doesn't fix it.
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS 10.13.2
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rlang_0.1.6 dplyr_0.7.4
loaded via a namespace (and not attached):
[1] compiler_3.4.0 magrittr_1.5 assertthat_0.2.0 R6_2.2.2
[5] tools_3.4.0 bindrcpp_0.2 glue_1.2.0 tibble_1.3.4
[9] yaml_2.1.16 Rcpp_0.12.14 pkgconfig_2.0.1 bindr_0.1
Upvotes: 3