Skip to content

Instantly share code, notes, and snippets.

@mmparker
Created February 17, 2023 09:14
Show Gist options
  • Save mmparker/c26833aeda1779c0d60f01d2f903e193 to your computer and use it in GitHub Desktop.
Save mmparker/c26833aeda1779c0d60f01d2f903e193 to your computer and use it in GitHub Desktop.
library(dplyr)
df_list <- list(
mutate(mtcars[1:5, ], jurisdiction = 'New Guernsey'),
mutate(iris[1:5, ], mean = mean(Sepal.Length)),
mutate(beaver1[1:5, ], jurisdiction = 'New Guernsey', mean = mean(temp))
)
varlabels <- tribble(~variable, ~new_label,
"jurisdiction", "US State",
"mean", "Mean (months)")
relabel <- function(df, varlabels) {
new_labels <- data.frame(variable = colnames(df)) %>%
left_join(varlabels, by = 'variable') %>%
mutate(labels_to_assign = coalesce(new_label, variable))
colnames(df) <- new_labels$labels_to_assign
df
}
lapply(df_list, relabel, varlabels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment