Filters the first or last observation for each by group.
Arguments
- dataset
Input dataset
The variables specified by the
order
and theby_vars
parameter are expected.- by_vars
Grouping variables
Default:
NULL
Permitted Values: list of variables created by
exprs()
- order
Sort order
Within each by group the observations are ordered by the specified order.
Permitted Values: list of variables or
desc(<variable>)
function calls created byexprs()
, e.g.,exprs(ADT, desc(AVAL))
- mode
Selection mode (first or last)
If
"first"
is specified, the first observation of each by group is included in the output dataset. If"last"
is specified, the last observation of each by group is included in the output dataset.Permitted Values:
"first"
,"last"
- check_type
Check uniqueness?
If
"warning"
or"error"
is specified, the specified message is issued if the observations of the input dataset are not unique with respect to the by variables and the order.Default:
"warning"
Permitted Values:
"none"
,"warning"
,"error"
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.
See also
Utilities for Filtering Observations:
count_vals()
,
filter_joined()
,
filter_relative()
,
max_cond()
,
min_cond()
Examples
library(dplyr, warn.conflicts = FALSE)
library(admiral.test)
data("admiral_ex")
# Select first dose for each patient
admiral_ex %>%
filter_extreme(
by_vars = exprs(USUBJID),
order = exprs(EXSEQ),
mode = "first"
) %>%
select(USUBJID, EXSEQ)
#> # A tibble: 254 x 2
#> USUBJID EXSEQ
#> <chr> <dbl>
#> 1 01-701-1015 1
#> 2 01-701-1023 1
#> 3 01-701-1028 1
#> 4 01-701-1033 1
#> 5 01-701-1034 1
#> 6 01-701-1047 1
#> 7 01-701-1097 1
#> 8 01-701-1111 1
#> 9 01-701-1115 1
#> 10 01-701-1118 1
#> # … with 244 more rows
# Select highest dose for each patient on the active drug
admiral_ex %>%
filter(EXTRT != "PLACEBO") %>%
filter_extreme(
by_vars = exprs(USUBJID),
order = exprs(EXDOSE),
mode = "last",
check_type = "none"
) %>%
select(USUBJID, EXTRT, EXDOSE)
#> # A tibble: 168 x 3
#> USUBJID EXTRT EXDOSE
#> <chr> <chr> <dbl>
#> 1 01-701-1028 XANOMELINE 81
#> 2 01-701-1033 XANOMELINE 54
#> 3 01-701-1034 XANOMELINE 81
#> 4 01-701-1097 XANOMELINE 54
#> 5 01-701-1111 XANOMELINE 54
#> 6 01-701-1115 XANOMELINE 54
#> 7 01-701-1133 XANOMELINE 81
#> 8 01-701-1146 XANOMELINE 81
#> 9 01-701-1148 XANOMELINE 81
#> 10 01-701-1180 XANOMELINE 81
#> # … with 158 more rows