dplyr rename – Error: `new_name` = old_name must be a symbol or a string, not formula

问题内容:

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'))

问题评论:

    
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?
    
If you get the error again, please run sessionInfo() immediately after you see the error pop up and post the output here.
    
rename_() version: df %>% dplyr::rename_('new_name' = 'old_name')
– Rassakhatsky Dmitry
35 mins ago
    
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.

原文地址:

https://stackoverflow.com/questions/47755534/dplyr-rename-error-new-name-old-name-must-be-a-symbol-or-a-string-not-fo

Tags:, , ,

添加评论

友情链接:蝴蝶教程