Skip to content

Instantly share code, notes, and snippets.

@sfirke
Created January 29, 2016 15:06
Show Gist options
  • Save sfirke/7561d18d9af8f16994bb to your computer and use it in GitHub Desktop.
Save sfirke/7561d18d9af8f16994bb to your computer and use it in GitHub Desktop.
Cleaning data.frame names with dplyr
clean_names <- function(dat){
# Takes a data.frame, returns the same data frame with cleaned names
old_names <- names(dat)
new_names <- old_names %>%
gsub("%", "percent", .) %>%
make.names(.) %>%
gsub("[.]+", "_", .) %>%
tolower(.) %>%
gsub("_$", "", .)
setNames(dat, new_names)
}
@sfirke
Copy link
Author

sfirke commented Oct 19, 2016

In case anyone ever sees this - it's now janitor::clean_names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment