Skip to content

Commit

Permalink
updated functionality
Browse files Browse the repository at this point in the history
allows for specifyying by_vars
  • Loading branch information
StefanThoma committed Sep 12, 2024
1 parent 3d4bdb9 commit 379c831
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions R/derive_vars_cat.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#' e.g. exprs(~condition, ~AVALCAT1, ~AVALCA1N
#' AVAL >= 140, ">=140 cm", 1,
#' AVAL < 140, "<140 cm", 2)
#' @param by_vars list of expressions with one element. `NULL` by default.
#' Allows for specifying a single category, e.g. `exprs(PARAMCD)`.
#'
#' @return data frame
#' @family der_gen
Expand Down Expand Up @@ -41,22 +43,48 @@
#' VSTEST == "Height" & AVAL > 140, ">140 cm", 1, "extra1",
#' VSTEST == "Height" & AVAL <= 140, "<=140 cm", 2, "extra2"
#' )
#'
#' derive_vars_cat(
#' dataset = advs %>% dplyr::filter(VSTEST == "Height"),
#' definition = definition
#' )
#'
#' definition2 <- exprs(
#' ~VSTEST, ~condition, ~AVALCAT1, ~AVALCA1N,
#' "Height", AVAL > 140, ">140 cm", 1,
#' "Height", AVAL <= 140, "<=140 cm", 2
#' )
#'
#' derive_vars_cat(
#' dataset = advs,
#' definition = definition2,
#' by_vars = exprs(VSTEST)
#' )

derive_vars_cat <- function(dataset,
definition) {
definition,
by_vars = NULL) {
# assertions
assert_data_frame(dataset)
assert_expr_list(definition)
assert_data_frame(dataset, required_vars = admiraldev::extract_vars(definition) %>% unique())
if(!is.null(by_vars)){
assert_expr_list(by_vars)
assert_data_frame(dataset, required_vars = c(admiraldev::extract_vars(definition) %>% unique(), extract_vars(by_vars)))

} else{
assert_data_frame(dataset, required_vars = admiraldev::extract_vars(definition) %>% unique())
}


# transform definition to tibble
names(definition) <- NULL
definition <- tibble::tribble(!!!definition)
assert_data_frame(definition, required_vars = exprs(condition))
if(!is.null(by_vars)){
assert_data_frame(definition, required_vars = c(exprs(condition), by_vars))
# add condition
definition <- definition %>% mutate(
condition = extend_condition(as.character(condition), as.character(by_vars), is = !!sym(as.character(by_vars))) %>% parse_exprs()
)
} else assert_data_frame(definition, required_vars = exprs(condition))


# extract new variable names and conditions
Expand All @@ -78,3 +106,10 @@ derive_vars_cat <- function(dataset,

return(new_dataset)
}

## helper

definition
extend_condition <- function(cond, var, is) {
paste(cond, " & ", var, " == '", is, "'", sep = "")
}

0 comments on commit 379c831

Please sign in to comment.