Skip to content

Instantly share code, notes, and snippets.

@chris-prener
Created January 27, 2022 16:34
Show Gist options
  • Save chris-prener/fa74e32c4836fe82b64967379dad1cff to your computer and use it in GitHub Desktop.
Save chris-prener/fa74e32c4836fe82b64967379dad1cff to your computer and use it in GitHub Desktop.
# batch edit column names based on a particular phrase
# store data in global environment, modify
mpg <- ggplot2::mpg
colnames(mpg) <- paste("depression", colnames(mpg), sep = "_")
# as a function
rename_by_phrase <- function(.data, pattern, replacement){
# modify variable names based on input
colnames(.data) <- stringr::str_replace_all(string = colnames(.data),
pattern = pattern,
replacement = replacement)
# return output
return(.data)
}
# test function
rename_by_phrase(mpg, pattern = "depression", replacement = "dep")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment