Skip to content

Commit

Permalink
initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanThoma committed Sep 3, 2024
1 parent 0beb82d commit 97b58c7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions R/derive_vars_cat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' Derive pair of variables
#'
#'
#' @param dataset
#' @param definition
#'
#' @return
#' @export
#'
#' @examples
#'
#' advs <- tibble::tribble(
#' ~USUBJID, ~PARAMCD, ~AVAL,
#' "01", "HEIGHT", 150,
#' "02", "HEIGHT", 135,
#' "03", "HEIGHT", 145
#' )
#'
#' define_tibble <- tribble(
#' ~condition, ~AVALCAT1, ~AVALCA1N, ~NEWCOL,
#' expr(AVAL > 140), ">140 cm", 1, "extra1",
#' expr(AVAL <= 140), "<=140 cm", 2, "extra2"
#' )
#'
#'
#' derive_vars_cat(dataset = advs, definition = define_tibble) %>% print()

derive_vars_cat <- function(dataset,
definition) {
# assertions (waiting for feedback first)

# extract new variable names
new_col_names <- names(expr_list)[!names(expr_list)=="condition"]

# (re)apply the function for each new variable name and iteratively derive the categories
new_dataset <- reduce(new_col_names, function(.data, col_name) {
# extract conditions
condition <- definition[["condition"]] # could also be outside of the function
# extract values
values <- definition[[col_name]]

.data %>%
mutate(!!sym(col_name) := eval(rlang::call2(
"case_when",
!!!map2(condition, values, ~ expr(!!.x ~ !!.y))
)))
}, .init = dataset)

return(new_dataset)
}

0 comments on commit 97b58c7

Please sign in to comment.