Search code examples
rtidyselect

How to use the new vars_rename function


I am trying to use the new suggested vars_rename() function in tidyselect after getting a soft warning that rename_at() in Dplyr is being deprecated. But, I'm having no luck getting it to work and the documentation is cryptic. It tells me to use list() instead of funs() and then shows an example with ~f(.) which doesn't really help.

I need to rename several columns at once. Previously, I would do it like this:

iris %>% rename_at(vars(starts_with("Sepal.")), funs(paste0(c("foobar.length", "foobar.width"))))

When I try the suggested tidyselect code

iris %>% vars_rename(starts_with("Sepal.") = c("foobar.width", "foobar.length"))

I get an error: unexpected '=' in "iris ..."

If I try the following

iris %>% vars_rename(starts_with("Sepal."), c("foobar.width", "foobar.length"))

I get an error: all arguments must be named.

I have tried multiple versions using list(). I can't make it work. I can still use the old code, but I'd like this to be reproducible in the future as it's an important study for my company. Any help is appreciated.


Solution

  • rename_at seems fine in this case, but the syntax changed a little.

    instead of funs one uses list, and adds a tilde to before paste0 in this case.
        old:
        iris %>% rename_at(vars(starts_with("Sepal.")), funs(paste0(c("foobar.length", "foobar.width"))))
        new:
        iris %>% rename_at(vars(starts_with("Sepal.")), list(~paste0(c("foobar.length", "foobar.width"))))