Expand description
Group-level iteration over a DataFrame key column.
Two rungs, cheapest first:
-
Typed rows, grouped Rust-side — after
Vec::<Row>::from_dataframe(&df)?, grouping is plain Rust.group_rowsmakes the idiom discoverable:ⓘlet rows: Vec<Obs> = Vec::from_dataframe(&df)?; let by_site = group_rows(rows, |r| r.site.clone()); // by_site: BTreeMap<String, Vec<Obs>> — plain Rust data, rayon-safe. -
Untyped, index-based —
DataFrame::group_bycomputes group indices once (single pass, main thread) without extracting rows:ⓘlet grouped = df.group_by("site")?; for (key, rows) in grouped.iter() { /* key: &GroupKey, rows: &[usize] */ } let mut out = NamedListBuilder::with_capacity(grouped.len()); for (key, sub) in grouped.frames() { // Transfer each rooted frame into the owned list. out.push(key.label(), sub); }
§Key semantics (vs R split())
- Group order: factor keys follow level order (empty levels kept, like
split()); character keys sort in byte order (R sorts in locale collation order — identical for ASCII); integer keys sort numerically; logical keys orderFALSE,TRUE; double keys (DateandPOSIXctincluded) sort numerically, withNaNafter every number. NAkeys form one group, ordered last — a deliberate deviation fromsplit(), which silently drops NA-keyed rows. A literal NA level (addNA(f)) also surfaces asGroupKey::Na.- Double keys group by value with dplyr’s (vctrs’) equality:
-0and0are one key, everyNaNis one key, andNaNandNAare separate groups. Labels are R’sas.character()of the key — seeRealKey. A bit64integer64column (int64 bits stored in a double vector) is an error.
§Composite keys (group_by_multi)
DataFrame::group_by_multi groups on several columns at once, keying by a
GroupKey::Tuple of the per-column scalar keys. Non-NA groups match
split(df, interaction(col1, col2, …, drop = TRUE)) (the first column varies
fastest, R interaction()’s default) — exactly, for keys whose byte order
coincides with the session’s collation (always true in the C locale;
single-case ASCII in practice — the character-key byte-order choice above
applies per column). Extending the single-column
NA convention, any tuple with an NA in any component forms its own trailing
group (first-encounter order) instead of being dropped as interaction() +
split() would.
Structs§
- Group
Declaration - How a dplyr-grouped frame declares its grouping, returned by
DataFrame::group_declaration: the grouping variables and the.droppolicy, without the cached group rows. - Grouped
Data Frame - A
DataFramepartitioned by the values of one key column. - Groups
Attr 🔒 - A grouped frame’s
groupsattribute after the structural checks ofDataFrame::groups_attr. - RealKey
- The key of one double-valued group (
GroupKey::Real): the value plus the text R prints for it.
Enums§
- Group
Key - The key of one group produced by
DataFrame::group_byorDataFrame::group_by_multi.
Constants§
- CANONICAL_
NAN_ 🔒BITS - Bits of the one
NaNeveryNaNkey is stored as: positive and quiet, sof64::total_cmporders it after+Infon every platform (x86’s0/0has the sign bit set), and its low word is not R’s NA payload (1954).
Functions§
- character_
groups 🔒 - Character keys: byte-order sort (BTreeMap), NA last.
- check_
rows_ 🔒partition - Check that in-range
.rowscover each of the frame’snrowrows exactly once — the shape dplyr always maintains. The first row claimed twice, or else the first row no group claims, is reported. - column_
groups 🔒 - One supported key column’s groups, in single-column group order (NA last)
— the per-type dispatch behind
DataFrame::group_by, and throughcolumn_keys/row_keysbehindgroup_by_multiandgroup_by_metadata. - column_
keys 🔒 - Per-row group key for one supported key column, in row order. NA cells — and
factor NA codes /
addNA()levels — becomeGroupKey::Na. Same type dispatch and errors asDataFrame::group_by. - factor_
groups 🔒 - Factor fast path: levels are the keys (level order, empty levels kept).
NA codes — and a literal NA level from
addNA()— land inGroupKey::Na. - group_
rows - Group already-extracted rows by a key function.
- group_
rows_ 🔒indices - Convert one
.rowslist element — an integer vector of 1-based row indices (DataFrame::groups_attrchecked its type) — into 0-based indices, rejecting any index outside1..=nrow(NAincluded). - integer_
groups 🔒 - Integer keys: numeric sort (BTreeMap), NA (
i32::MIN) last. - logical_
groups 🔒 - Logical keys:
FALSEthenTRUE(R’s sort order), NA last. Only keys present in the data appear. - normalize_
real 🔒 - Map a non-NA double to its key representative:
-0.0→0.0, anyNaN→ the canonicalNaN. Equal keys (by vctrs’ rules) then share their bits. - r_
typeof 🔒 - R’s
typeof()ofx:"double","NULL","list", …. - real_
groups 🔒 - Double keys (plain,
Date,POSIXct, other classed doubles): numeric sort withNaNafter every number,NA_real_last. Equality followsRealKey(-0 == 0, oneNaNkey); labels come from oneas.character()call over the distinct keys (real_labels). - real_
labels 🔒 - R’s
as.character()ofvaluescarryingcolumn’s attributes (class,tzone,units, …): the text R prints for each key, with S3 dispatch for a classed column (Date,POSIXct, …) and C-level coercion otherwise. - row_
keys 🔒 - Invert groups into one key per row (
nrowrows, row order). Each row of a key column lands in exactly one group, so every slot is filled.