## 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 order and the by_vars parameter are expected. by_vars Grouping variables Default: NULL Permitted Values: list of variables created by vars() order Sort order Within each by group the observations are ordered by the specified order. Permitted Values: list of variables or ⁠desc()⁠ function calls created by vars(), e.g., vars(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.

### Value

A dataset containing the first or last observation of each by group

### Author(s)

Stefan Bundfuss

Utilities for Filtering Observations: count_vals(), filter_confirmation(), filter_relative(), max_cond(), min_cond()

### Examples

library(dplyr, warn.conflict = FALSE)

# Select first dose for each patient
filter_extreme(
by_vars = vars(USUBJID),
order = vars(EXSEQ),
mode = "first"
) %>%
select(USUBJID, EXSEQ)

# Select highest dose for each patient on the active drug