filter_extreme {admiral} | R Documentation |
Filter the First or Last Observation for Each By Group
Description
Filters the first or last observation for each by group.
Usage
filter_extreme(dataset, by_vars = NULL, order, mode, check_type = "warning")
Arguments
dataset |
Input dataset The variables specified by the |
by_vars |
Grouping variables Default: Permitted Values: list of variables created by |
order |
Sort order Within each by group the observations are ordered by the specified order. Permitted Values: list of expressions created by |
mode |
Selection mode (first or last) If Permitted Values: |
check_type |
Check uniqueness? If Default: Permitted Values: |
Details
For each group (with respect to the variables specified for the
by_vars
parameter) the first or last observation (with respect to the
order specified for the order
parameter and the mode specified for the
mode
parameter) is included in the output dataset.
Value
A dataset containing the first or last observation of each by group
See Also
Utilities for Filtering Observations:
count_vals()
,
filter_exist()
,
filter_joined()
,
filter_not_exist()
,
filter_relative()
,
max_cond()
,
min_cond()
Examples
library(dplyr, warn.conflicts = FALSE)
ex <- tribble(
~STUDYID, ~DOMAIN, ~USUBJID, ~EXSEQ, ~EXDOSE, ~EXTRT,
"PILOT01", "EX", "01-1442", 1, 54, "XANO",
"PILOT01", "EX", "01-1442", 2, 54, "XANO",
"PILOT01", "EX", "01-1442", 3, 54, "XANO",
"PILOT01", "EX", "01-1444", 1, 54, "XANO",
"PILOT01", "EX", "01-1444", 2, 81, "XANO",
"PILOT01", "EX", "05-1382", 1, 54, "XANO",
"PILOT01", "EX", "08-1213", 1, 54, "XANO",
"PILOT01", "EX", "10-1053", 1, 54, "XANO",
"PILOT01", "EX", "10-1053", 2, 54, "XANO",
"PILOT01", "EX", "10-1183", 1, 0, "PLACEBO",
"PILOT01", "EX", "10-1183", 2, 0, "PLACEBO",
"PILOT01", "EX", "10-1183", 3, 0, "PLACEBO",
"PILOT01", "EX", "11-1036", 1, 0, "PLACEBO",
"PILOT01", "EX", "11-1036", 2, 0, "PLACEBO",
"PILOT01", "EX", "11-1036", 3, 0, "PLACEBO",
"PILOT01", "EX", "14-1425", 1, 54, "XANO",
"PILOT01", "EX", "15-1319", 1, 54, "XANO",
"PILOT01", "EX", "15-1319", 2, 81, "XANO",
"PILOT01", "EX", "16-1151", 1, 54, "XANO",
"PILOT01", "EX", "16-1151", 2, 54, "XANO"
)
# Select first dose for each patient
ex %>%
filter_extreme(
by_vars = exprs(USUBJID),
order = exprs(EXSEQ),
mode = "first"
) %>%
select(USUBJID, EXSEQ)
# Select highest dose for each patient on the active drug
ex %>%
filter(EXTRT != "PLACEBO") %>%
filter_extreme(
by_vars = exprs(USUBJID),
order = exprs(EXDOSE),
mode = "last",
check_type = "none"
) %>%
select(USUBJID, EXTRT, EXDOSE)