Skip to content

Instantly share code, notes, and snippets.

@nrkoehler
Last active October 12, 2022 08:07
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/d81ef273b86e72dd66a24c53a42ddeee to your computer and use it in GitHub Desktop.
Save nrkoehler/d81ef273b86e72dd66a24c53a42ddeee to your computer and use it in GitHub Desktop.
st_set_varlabs <- function(data,
dict = ITEMDESC,
view = modul,
keeps = PAGE_TABLE,
name = ITEM_COLUMN,
label = ITEM_LABEL) {
library(dplyr, warn.conflicts = FALSE)
dict <- dict %>%
filter({{keeps}} == view) %>%
mutate({{name}} := stringr::str_to_upper({{name}})) %>%
select({{name}}, {{label}}) %>%
distinct() %>%
arrange({{name}}) %>%
identity()
temp <- dict %>%
filter({{name}} %in% colnames(data))
vars <- pull(temp, {{name}})
labs <- pull(temp, {{label}})
length.diff <- ncol(data) - length(labs)
vars.all <- colnames(data)
vars.first <- c("PATIENT_ID", "VISITNAME", "VISIT_LABEL")
labs <- c(labs, rep("", length.diff))
data %>%
relocate(any_of(vars)) %>%
labelled::set_variable_labels(.labels = labs) %>%
relocate(any_of(vars.all)) %>%
relocate(any_of(vars.first)) %>%
identity()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment