dplyr rename - Error: `new_name` = old_name must be a symbol or a string, not formula
Asked Answered
S

2

19

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'))
Sled answered 11/12, 2017 at 14:53 Comment(4)
Might have to do with using new_name and old_name in dplyr and 'new_name' and 'old_name' (as text) in plyr. Can you also post the rename_() version that didn't work with dplyr? When it throws that error what do you get when you run just new_name and old_name?Thirtyeight
If you get the error again, please run sessionInfo() immediately after you see the error pop up and post the output here.Citarella
rename_() version: df %>% dplyr::rename_('new_name' = 'old_name')Sled
I can reproduce the error only after installing the development version of rlang. Things work find with the CRAN version of rlang on R 3.4.2.Transistorize
R
16

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.

Rowlett answered 7/1, 2018 at 15:50 Comment(1)
FWIW I solved this by updating all tidyverse packages one at a time, as well as magrittr and rlang.Conductive
H
3

I had the same problem. After updating all packages just in case, it works (see sessionInfo() below.

Fix

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       
Hourglass answered 23/12, 2017 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.