-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0beb82d
commit 97b58c7
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) | ||
} |