Find rows with maxima/minima in given columns.

max_by(.data, .col, ...)

# S3 method for ExprBuilder
max_by(
  .data,
  .col,
  ...,
  .some = FALSE,
  .chain = getOption("table.express.chain", TRUE)
)

# S3 method for data.table
max_by(.data, .col, ..., .expr = FALSE)

min_by(.data, .col, ...)

# S3 method for ExprBuilder
min_by(
  .data,
  .col,
  ...,
  .some = FALSE,
  .chain = getOption("table.express.chain", TRUE)
)

# S3 method for data.table
min_by(.data, .col, ..., .expr = FALSE)

Arguments

.data

An instance of ExprBuilder.

.col

A character vector indicating the columns that will be searched for extrema.

...

Optionally, columns to group by, either as characters or symbols.

.some

If TRUE the rows where any of the columns specified in .col have extrema are returned.

.chain

Logical. Should a new frame be automatically chained to the expression if the clause being set already exists?

.expr

If the input is a data.table and .expr is TRUE, an instance of EagerExprBuilder will be returned. Useful if you want to add clauses to j, e.g. with mutate-table.express.

Details

These verbs implement the idiom shown here by leveraging nest_expr(). The whole nested expression is assigned to i in the data.table's frame. It is probably a good idea to use this on a frame that has no other frames preceding it in the current expression, given that nest_expr() uses the captured data.table, so consider using chain() when needed.

Several columns can be specified in .col, and depending on the value of .some, the rows with all or some extrema are returned, using & or | respectively. Depending on your data, using more than one column might not make sense, resulting in an empty data.table.

Examples


data("mtcars")

data.table::as.data.table(mtcars) %>%
    max_by("mpg", "vs")
#>     mpg cyl  disp hp drat    wt qsec vs am gear carb
#> 1: 26.0   4 120.3 91 4.43 2.140 16.7  0  1    5    2
#> 2: 33.9   4  71.1 65 4.22 1.835 19.9  1  1    4    1