Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: derive_vars_transposed to preserve expected variables #2648

Open
yurovska opened this issue Jan 18, 2025 · 0 comments
Open
Labels
enhancement New feature or request programming

Comments

@yurovska
Copy link

Feature Idea

It would be nice if derive_vars_transposed could create the exact set of variables that are expected in the output even if some keys are missing in the data for the moment.

Relevant Input

No response

Relevant Output

No response

Reproducible Example/Pseudo Code

library(dplyr)
library(admiral)

# Example EX with Up-Titration scheme by Visit

ex <- admiral::ex_single %>%
  filter(
    USUBJID == "01-701-1097"
  ) %>%
  mutate(
    EXDOSE = case_when(
      VISIT == "BASELINE" ~ 2,
      VISIT == "WEEK 2" ~ 4,
      VISIT == "WEEK 24" ~ 8
    )
  )

# Example ADSL with the same subject

adsl <- admiral::admiral_adsl %>%
  filter(
    USUBJID == "01-701-1097"
  )

# Derive Study Day of First Dose for each dose level (TR2MGDY / TR4MGDY / TR8MGDY)

adsl_test1 <-  adsl %>%
  derive_vars_transposed(
    dataset_merge = 
      ex %>%
      group_by(USUBJID, EXDOSE) %>%
      slice_min(EXSTDY, n = 1) %>%
      mutate(
        TRXMGDY_KEY = paste0("TR", EXDOSE, "MGDY")
      ) %>%
      ungroup(),
    by_vars = exprs(USUBJID),
    key_var = TRXMGDY_KEY,
    value_var = EXSTDY,
    filter = TRXMGDY_KEY %in% c("TR2MGDY", "TR4MGDY", "TR8MGDY")
  )

# Perfect, all 3 variables are there in ADSL now

adsl_test1 %>%
  select(USUBJID, TR2MGDY, TR4MGDY, TR8MGDY)

# But let's say no subjects have reached 8 mg yet

ex_no_8mg <- ex %>%
  filter(
    EXDOSE != 8
  )

# Call `derive_vars_transposed` again

adsl_test2 <-  adsl %>%
  derive_vars_transposed(
    dataset_merge = 
      ex_no_8mg %>%
      group_by(USUBJID, EXDOSE) %>%
      slice_min(EXSTDY, n = 1) %>%
      mutate(
        TRXMGDY_KEY = paste0("TR", EXDOSE, "MGDY")
      ) %>%
      ungroup(),
    by_vars = exprs(USUBJID),
    key_var = TRXMGDY_KEY,
    value_var = EXSTDY,
    filter = TRXMGDY_KEY %in% c("TR2MGDY", "TR4MGDY", "TR8MGDY")
  )

# TR8MGDY is not there now
# and there is no way to preserve it within `derive_vars_transposed` call

adsl_test2 %>%
  select(USUBJID, TR2MGDY, TR4MGDY, TR8MGDY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request programming
Development

No branches or pull requests

1 participant