Add levels argument to add_difference() (tbl_summary + tbl_svysummary) to compare two groups (#2151)#2423
Merged
Merged
Conversation
Add a levels argument to add_difference.tbl_summary() so users can select which two by-variable groups to compare. This makes add_difference() usable when tbl_summary(by=) has more than two levels, and lets users flip the direction of the difference for two-level by variables. The difference is computed as levels[1] minus levels[2]. When supplied, the data is subset to the two selected levels with the by factor re-leveled, while the full set of summary statistic columns is retained. A footnote names the compared pair and direction. Co-authored-by: Ona <no-reply@ona.com>
The levels argument subset x$inputs$data for the difference calculation but left the subset on the returned object, permanently discarding the other groups' data. Capture the original data before subsetting and restore it before returning, so the calculation uses the subset while the returned object keeps the full data. Add a regression test. Co-authored-by: Ona <no-reply@ona.com>
Owner
Author
|
Hi @larmarange , hope you've been well. Do you want to take a look at this? You were interested in this functionality to reverse the difference calculation for your before-after tables. |
Co-authored-by: Ona <no-reply@ona.com>
Mirrors the levels argument added to add_difference.tbl_summary(): select which two by groups to compare (levels[1] - levels[2]), required when by has 3+ levels and able to flip direction for two-level by. The shared .process_difference_levels() helper is generalized to handle survey designs (subset and re-level via survey::subset, restoring stripped column labels). Co-authored-by: Ona <no-reply@ona.com>
levels argument to add_difference.tbl_summary() to compare two groups (#2151)levels argument to add_difference() (tbl_summary + tbl_svysummary) to compare two groups (#2151)
The levels direction footnote (e.g. 'Difference: I - III') was overwriting the footnote describing the difference method/test. Use the internal .modify_footnote_header(replace = FALSE) so both footnotes are shown. Co-authored-by: Ona <no-reply@ona.com>
12 tasks
larmarange
approved these changes
Jul 8, 2026
larmarange
left a comment
Collaborator
There was a problem hiding this comment.
Thanks a lot @ddsjoberg
Everything looks right to me
Owner
Author
|
Thank you @larmarange ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes are proposed in this pull request?
Added a
levelsargument toadd_difference.tbl_summary()andadd_difference.tbl_svysummary()to select which twobygroups to compare. This makesadd_difference()usable whenby=has more than two levels, and lets users flip the direction of the difference for two-levelbyvariables. (#2151)The difference is computed as
levels[1]minuslevels[2]. When supplied, the data is subset to the two selected levels with thebyfactor re-leveled, while all summary statistic columns are retained. A footnote names the compared pair and direction.levelsargument (defaultNULL), a length-2 vector ofby-level values matched by value.byhas 3+ levels; the previous "exactly two levels" error now points users tolevels.by, where reversing the pair flips the sign.bylevels (error lists valid levels)..process_difference_levels()) for validation and subsetting. The helper is survey-aware: fortbl_svysummary()it subsets and re-levels thesurvey.design(viasurvey::subset()), restoring column labels that subsetting strips.If there is an GitHub issue associated with this pull request, please provide link.
Closes #2151
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()usethis::use_spell_check()runs with no spelling errors in documentationWhen the branch is ready to be merged into master:
NEWS.mdwith the changes from this pull request under the heading "# gtsummary (development version)". If there is an issue associated with the pull request, reference it in parentheses at the end update (seeNEWS.mdfor examples).usethis::use_version(which = "dev")usethis::use_spell_check()again