Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using an expression to compute the new name in rename #1600

Closed
ggrothendieck opened this issue Dec 27, 2015 · 4 comments
Closed

Using an expression to compute the new name in rename #1600

ggrothendieck opened this issue Dec 27, 2015 · 4 comments
Labels
feature a feature request or enhancement
Milestone

Comments

@ggrothendieck
Copy link

This works to rename the demand column to demand2. Note we are using plyr's rename, not dplyr's rename:

library(dplyr)
num <- 2
BOD %>% plyr::rename(list(demand = paste0("demand", num)))  # ok

Since dplyr's rename uses new = old vs. plyr which uses old = new there is no easy way to do the above with dplyr's rename. This is pretty ugly but anyways it gives an error message:

library(dplyr)
num <- 2
BOD %>% do({ L <- setNames(list("demand"), paste0("demand", num)); rename_(., L) }) # bad

Also the following gives an error message since one cannot provide an expression where a name goes:

library(dplyr)
num <- 2
BOD %>% rename(list(paste0("demand", num) = demand))  # bad

Actually neither plyr's rename nor dplyr's rename are fully general since plyr can only easily handle expressions in the new name and dplyr in the old name. renameCol in the doBy package can handle expressions in both the old and the new name as it uses the syntax renameCol(indata, src, tgt) where src and tgt are character vectors of names and either of these or both can be complex expressions.

# rename column named paste0(base, num) to toupper(base)
# (In this case it renames column x2 to X)
library(dplyr)
base <- "x"
num <- 2
anscombe %>% doBy::renameCol(paste0(base, num), toupper(base))
@hadley hadley added this to the 0.6 milestone Mar 1, 2016
@hadley hadley added the feature a feature request or enhancement label Mar 1, 2016
@hadley
Copy link
Member

hadley commented Feb 16, 2017

Will be possible once tidyeval conversion is complete. Syntax will look something like this:

old_var <- "x"
new_var <- "y"

df %>% rename(!!old_var ~ !!new_var)

We'll have plenty of documentation.

@hadley hadley closed this as completed Feb 16, 2017
@FelixTheStudent
Copy link

Hi all,

is this or a similar solution now implemented?

new_name <- "y"
mtcars %>% dplyr::rename( mpg ~ !!new_name  )

gives me the following error with dplyr version 0.7.4:

Error: All arguments must be named

Thanks,

Felix  
 

@lionel-
Copy link
Member

lionel- commented Nov 22, 2017

this should be:

dplyr::rename(mtcars, !! new_name := mpg)

Using := instead of = to allow for !! on the left-hand side.

@FelixTheStudent
Copy link

thanks a bunch, that works. I simultaneously found it in vignette("programming"), very nicely written! Love the tidyverse.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants