Skip to content

Instantly share code, notes, and snippets.

@nrkoehler
Last active October 12, 2022 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrkoehler/1a6c1e99860c36c64a49bfbba04c38f8 to your computer and use it in GitHub Desktop.
Save nrkoehler/1a6c1e99860c36c64a49bfbba04c38f8 to your computer and use it in GitHub Desktop.
df.VARLABS <- tibble::tribble(
~VARNAME, ~LABEL,
"PATIENT_ID", "Patient ID",
"ARM", "Randomisation arm",
"RANDO_DAT", "Randomisation date"
)
df.DATA <- data.frame(
PATIENT_ID = "id-123",
ARM = "Intervention",
RANDO_DAT = "2022-02-03",
NO_LABEL = "no label in dictionary"
)
set_varlabs <- function(data,
dict = df.VARLABS,
name = VARNAME,
label = LABEL
) {
labs <- dict %>%
filter({{name}} %in% colnames(data)) %>%
pull({{label}})
length.diff <- ncol(data) - length(labs)
if (length.diff > 0) {labs <- c(labs, rep("", length.diff))}
data %>%
relocate(any_of(pull(dict, {{name}}))) %>%
labelled::set_variable_labels(.labels = labs)
}
df.DATA_W_LABELS <- df.DATA %>%
set_varlabs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment