Closed ggrothendieck closed 7 years ago
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.
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
this should be:
dplyr::rename(mtcars, !! new_name := mpg)
Using :=
instead of =
to allow for !!
on the left-hand side.
thanks a bunch, that works. I simultaneously found it in vignette("programming")
, very nicely written! Love the tidyverse.
This works to rename the
demand
column todemand2
. Note we are using plyr'srename
, not dplyr'srename
:Since dplyr's
rename
uses new = old vs. plyr which uses old = new there is no easy way to do the above with dplyr'srename
. This is pretty ugly but anyways it gives an error message:Also the following gives an error message since one cannot provide an expression where a name goes:
Actually neither plyr's
rename
nor dplyr'srename
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 syntaxrenameCol(indata, src, tgt)
wheresrc
andtgt
are character vectors of names and either of these or both can be complex expressions.