Add by_level argument to sort_ard_hierarchical() (#548) - #591
Conversation
Adds a `by_level` argument to `sort_ard_hierarchical()` allowing `"descending"` sorting to rank variable groups by the counts observed within specific `by` variable levels, rather than the sums across all levels. The argument accepts a named list (names are `by` variables, values are a single level each), so any combination of `by` variables may be used. This generalizes the single-`by`-variable approach explored in #550 to work with any number of `by` variables, integrated into the current vctrs-based sort engine: a per-row mask built from the pre-reformat `by` group columns is AND-ed into the descending count-sum filter. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Unit Tests Summary 1 files 276 suites 19s ⏱️ Results for commit 0ed7ca5. ♻️ This comment has been updated with latest results. |
Code Coverage SummaryDiff against mainResults for commit: 0ed7ca5 Minimum allowed coverage is ♻️ This comment has been updated with latest results |
Unit Test Performance DifferenceAdditional test case details
Results for commit 45db895 ♻️ This comment has been updated with latest results. |
|
Hi @rikoprogrammer, your branch had a merge conflict, and rather than trying to resolve it, I just made a new branch. Can you take a look here? Did I generalize your code correctly? |
Hi @ddsjoberg, yes, the code is well generalized. Thank you |
What changes are proposed in this pull request?
by_levelargument tosort_ard_hierarchical(), allowing"descending"sorting to rank variable groups by the counts observed within specificbyvariable levels (e.g.by_level = list(TRTA = "Placebo")) rather than the sums across allbyvariable levels. The argument accepts a named list, so any combination ofbyvariables may be used. (Feature Request: sort_ard_hierarchical #548, @rikoprogrammer)Provide more detail here as needed.
by_levelaccepts a named list where names arebyvariables and values are a single level of that variable. When sorting"descending", variable groups are ranked by count sums computed only from rows matching the specifiedbylevel(s):This generalizes the single-
by-variablesort_levelapproach explored in #550 to work with any number ofbyvariables, which was that PR's core limitation.Implementation notes for reviewers
Since #550 was opened, the sort engine was rewritten for performance (#176), so
.append_hierarchy_sums()no longer exists. The feature is integrated into the currentvctrs-based engine:by(as supplied toard_stack_hierarchical()) is captured before the "highest-severity" extraction that moves the innermostbyvariable intovariables, soby_levelis validated and applied against thebyvariables as the user knows them..hierarchy_by_level_mask()builds a per-row logical mask from the pre-reformatbygroup columns (which are not reordered by the sort), keeping it positionally aligned with the reformatted frame. It is AND-ed into thekeepfilter of.hierarchy_sort_group_desc(), so only rows at the requested level(s) contribute to the count sums. Variable groups with no contributing rows sort last..check_by_level()validates the input: must be a fully-named list, names must bebyvariables, a single level per entry, the level must exist, and at least onebyvariable must be present.Correctness was verified with an oracle: sorting with
by_level = list(TRTA = "Placebo")on the full ARD produces the same variable-group ordering as the default sort on an ARD built only from Placebo subjects — confirmed for both single- and two-by-variable cases. New unit tests cover the functional behavior, multiplebyvariables,by_level = NULLequivalence to the default, and all validation error paths (snapshots added).closes #548
Pre-review Checklist (if item does not apply, mark is as complete)
usethis::pr_merge_main()(branched from currentmain)devtools::test_coverage()Reviewer Checklist (if item does not apply, mark is as complete)
pkgdown::build_site(). Check the R console for errors, and review the rendered website.devtools::test_coverage()When the branch is ready to be merged:
NEWS.mdwith the changes from this pull request under the heading "# cards (development version)". If there is an issue associated with the pull request, reference it in parentheses at the end update (seeNEWS.mdfor examples).