Skip to content

Commit

Permalink
#2247 - Update derive_vars_transposed() with relationship argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
sophie-gem committed May 19, 2024
1 parent d2a0383 commit 9197e43
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions R/derive_vars_transposed.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
#'
#' @param filter Expression used to restrict the records of `dataset_merge` prior to transposing
#'
#' @param relationship Expected merge-relationship between the `by_vars`
#' variable(s) in `dataset` and `dataset_merge` (after transposition)
#'
#' This argument is passed to the `dplyr::left_join()` function. See
#' https://dplyr.tidyverse.org/reference/mutate-joins.html#arguments for
#' more details.
#'
#' Permitted Values for `relationship`: `"one-to-one"`, `"one-to-many"`,
#' `"many-to-one"`, `"many-to-many"`, `NULL`.
#'
#' @details
#' After filtering `dataset_merge` based upon the condition provided in `filter`, this
#' dataset is transposed and subsequently merged onto `dataset` using `by_vars` as
Expand Down Expand Up @@ -84,19 +94,25 @@ derive_vars_transposed <- function(dataset,
by_vars,
key_var,
value_var,
filter = NULL) {
filter = NULL,
relationship = NULL) {
key_var <- assert_symbol(enexpr(key_var))
value_var <- assert_symbol(enexpr(value_var))
filter <- assert_filter_cond(enexpr(filter), optional = TRUE)
assert_vars(by_vars)
assert_data_frame(dataset, required_vars = replace_values_by_names(by_vars))
assert_data_frame(dataset_merge, required_vars = expr_c(by_vars, key_var, value_var))
relationship <- assert_character_scalar(
relationship,
values = c("one-to-one", "one-to-many", "many-to-one", "many-to-many"),
case_sensitive = TRUE,
optional = TRUE)

dataset_transposed <- dataset_merge %>%
filter_if(filter) %>%
pivot_wider(names_from = !!key_var, values_from = !!value_var)

left_join(dataset, dataset_transposed, by = vars2chr(by_vars))
left_join(dataset, dataset_transposed, by = vars2chr(by_vars), relationship = relationship)
}

#' Derive ATC Class Variables
Expand Down

0 comments on commit 9197e43

Please sign in to comment.