R dplyr: rename variables using string functions
This is a very late answer, on May 2017
As of dplyr 0.5.0.9004
, soon to be 0.6.0, many new ways of renaming columns, compliant with the maggritr
pipe operator %>%
, have been added to the package.
Those functions are:
- rename_all
- rename_if
- rename_at
There are many different ways of using those functions, but the one relevant to your problem, using the stringr
package is the following:
df <- df %>%
rename_all(
funs(
stringr::str_to_lower(.) %>%
stringr::str_replace_all(., '\\.', '_')
)
)
And so, carry on with the plumbing :) (no pun intended).
Here's a way around the somewhat awkward rename
syntax:
myris <- iris %>% setNames(tolower(gsub("\\.","_",names(.))))
I think you're looking at the documentation for plyr::rename
, not dplyr::rename
. You would do something like this with dplyr::rename
:
iris %>% rename_(.dots=setNames(names(.), tolower(gsub("\\.", "_", names(.)))))