library(tibble)
response <- tribble(
~USUBJID, ~AVISITN, ~AVALC,
"1", 1, "PR",
"1", 2, "CR",
"1", 3, "CR",
"1", 4, "SD",
"1", 5, "NE",
"2", 1, "SD",
"2", 2, "PD",
"2", 3, "PD",
"3", 1, "SD",
"4", 1, "SD",
"4", 2, "PR",
"4", 3, "PD",
"4", 4, "SD",
"4", 5, "PR"
)
# Select observations up to first PD for each patient
response %>%
filter_relative(
by_vars = exprs(USUBJID),
order = exprs(AVISITN),
condition = AVALC == "PD",
mode = "first",
selection = "before",
inclusive = TRUE
)
# Select observations after last CR, PR, or SD for each patient
response %>%
filter_relative(
by_vars = exprs(USUBJID),
order = exprs(AVISITN),
condition = AVALC %in% c("CR", "PR", "SD"),
mode = "last",
selection = "after",
inclusive = FALSE
)
# Select observations from first response to first PD
response %>%
filter_relative(
by_vars = exprs(USUBJID),
order = exprs(AVISITN),
condition = AVALC %in% c("CR", "PR"),
mode = "first",
selection = "after",
inclusive = TRUE,
keep_no_ref_groups = FALSE
) %>%
filter_relative(
by_vars = exprs(USUBJID),
order = exprs(AVISITN),
condition = AVALC == "PD",
mode = "first",
selection = "before",
inclusive = TRUE
)
Run the code above in your browser using DataLab