Created
January 27, 2022 16:34
-
-
Save chris-prener/fa74e32c4836fe82b64967379dad1cff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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