median

Computes the median of a sheet, optionally grouped by rows or cols, skipping "-" and undefined.

import { Sheet } from "@teakit/sheet";

Sheet.median({ a: [3, 1, 2] }); // 2   (odd count → middle value)
Sheet.median({ a: [1, 2, 3, 4] }); // 2.5 (even count → mean of two middles)

Sheet.median({ a: [5, "-", 1, undefined, 3] }); // 3 (skips "-"/undefined)

Sheet.median({ a: [1, 2, 3], b: [10, 20, 30, 40] }, { by: "rows" });
// { a: [2], b: [25] }

Sheet.median({ a: [] }, { by: "rows" }); // { a: ["-"] }

// `using` may override the median semantics.
Sheet.median({ a: [1, 2, 3, 4] }, { using: (values) => values[0] }); // 1

API Reference

Signature

Sheet.median<TCell, TKey extends string, TBy extends SheetAggregateBy>(
  sheet: Sheet<TCell, TKey>,
  options?: SheetAggregateOptions<TBy, SheetValueOf<TCell>, TResult>,
): SheetAggregateResult<TKey, TBy, TResult>;

Parameters

ParameterTypeRequiredNotes
options.by"all" | "rows" | "cols"NoDefaults to "all".
options.using(values) => TResultNoReceives only valid values. Defaults to the numeric median.

Returns

Same shape as sum, depending on by. The default sorts valid values ascending; an even count averages the two middle values. Empty buckets are "-".

Throws

  • SHEET_INVALID_AGGREGATE_OPTIONS — invalid by or non-function using.

Agent Contract

FieldValue
Kindstatic aggregation
Canonical namemedian
AliasesNone
Mutates inputsNo
ReturnsSheetAggregateResult<TKey, TBy, TResult>

Agent Notes

  • The default median handles number only; supply using for custom types.
  • A using override is not required to preserve median semantics.