Set a listwise complete-case filter for matching N across analyses
Source:R/pipeline.R
jcomplete.Rdjcomplete() registers a set of variables and activates a listwise
deletion filter that excludes any case with a missing value on any of
the registered variables. This ensures that all subsequent analyses use
the same set of complete cases, which is essential when preliminary
analyses need to match the N of a final regression model.
The setting is stored per dataset, so switching juse() between
datasets preserves each dataset's setting independently.
The jcomplete filter applies whenever the matching dataset is used,
regardless of whether it was supplied via juse() or specified
explicitly in a function call. To bypass temporarily without losing
the setting, use jcomplete(off) before the analysis and
jcomplete(on) afterward. This matches the SPSS USE ALL /
FILTER convention.
If a variable in the setting is later dropped from the dataset or
renamed, each analysis of that dataset stops, naming the variable, until
jcomplete() is run again with the current names or the setting is
cleared. jcomplete(on) and a preview of the set filter stop in the
same way, and the status display (jcomplete()) names the variable
in place of the complete-case count.
Usage
jcomplete(
data,
...,
preview = FALSE,
console = FALSE,
non.deletes = FALSE,
clear.all = FALSE
)Arguments
- data
A data frame. If omitted, uses the default set by
juse(). Instead of variable names, the call may carry one of three special values: the bare wordoffdeactivates the setting but remembers the variables,onreactivates it, andNULLclears it entirely. Each acts on thejuse()default dataset whendatais omitted (jcomplete(off),jcomplete(NULL)), or on the named dataset when it is given (jcomplete(d, off),jcomplete(d, NULL)). With no default set,jcomplete(NULL)clears the one dataset that carries a setting, and asks you to name one when several do. A setting is stored under its dataset's name, so the namedoffandNULLstill work after that dataset has been removed, or its name given to something that is not a data frame;onis refused there, since the setting cannot be checked without its dataset. Call with no arguments to check the current status.- ...
Unquoted variable names to include in the listwise check.
- preview
Logical. If
TRUE, open a viewer (RStudio data tab) showing the rows the listwise filter will drop, with a leadingRowcolumn (original data position) and a trailingDeletionCheckflag (1 = the row will be dropped). May be used on its own to preview the already-set filter without re-listing the variables (jcomplete(preview = TRUE)). DefaultFALSE.- console
Logical or numeric. Print the dropped rows to the console.
TRUEprints the first 10; a number prints that many. Independent ofpreview: on its own it prints to the console without opening the viewer; combine withpreview = TRUEto get both. The console listing is always limited to the dropped rows so it cannot flood the console. DefaultFALSE.- non.deletes
Logical. If
TRUE, the viewer shows every case (withDeletionCheckmarking which will drop) rather than only the dropped rows. Affects the viewer only; the console listing stays deleted-rows-only. DefaultFALSE.- clear.all
Logical. If
TRUE, clears the jcomplete setting on every dataset; use on its own,jcomplete(clear.all = TRUE). This is the same grammar asjsubset()and the registration functions (jdummy(),jnumeric(),jcount(),jlikert()):NULLclears one dataset,clear.all = TRUEclears them all.
Value
Invisibly returns NULL. When a preview is requested,
invisibly returns the previewed data frame instead, so it can be
captured (e.g. jcomplete_rows <- jcomplete(preview = TRUE)).
See also
jstats for the package overview,
workflow conventions, and complete function listing.
Examples
juse(community)
#> Default data frame set to: community
jcomplete(Income, Education, Age)
#> Listwise Case Filter
#> Using default data frame: community
#>
#> Variable N Missing % Missing
#> --------- --- ------- ---------
#> Income 103 6 5.8%
#> Education 103 6 5.8%
#> Age 103 0 0.0%
#>
#> Complete cases: 91 of 103 (88.3%)
#> Listwise filter activated -- 12 cases will be excluded from
#> subsequent analyses.
jcomplete(community, Income, Education, Age) # Explicit dataset
#> Listwise Case Filter
#> Variable N Missing % Missing
#> --------- --- ------- ---------
#> Income 103 6 5.8%
#> Education 103 6 5.8%
#> Age 103 0 0.0%
#>
#> Complete cases: 91 of 103 (88.3%)
#> Listwise filter activated -- 12 cases will be excluded from
#> subsequent analyses.
jcomplete(data = community, Income, Education, Age) # ... given by name
#> Listwise Case Filter
#> Variable N Missing % Missing
#> --------- --- ------- ---------
#> Income 103 6 5.8%
#> Education 103 6 5.8%
#> Age 103 0 0.0%
#>
#> Complete cases: 91 of 103 (88.3%)
#> Listwise filter activated -- 12 cases will be excluded from
#> subsequent analyses.
jdesc(Age) # Uses only complete cases on those 3 vars
#> Descriptive Statistics
#> Using default data frame: community
#>
#> Case Processing Excluded Remaining
#> Original -- 103
#> jcomplete() 12 91 Income, Education, +1 more
#> Remaining N -- 91
#> ----------------------------------------------------------------
#>
#> Variable Total Non_missing Min Max Mean SD
#> -------- ----- ----------- --- --- ------ ------
#> Age 91 91 18 71 40.484 11.933
#>
jcomplete(Income, Education, Age, preview = TRUE) # Set and preview together
#> Listwise Case Filter
#> Using default data frame: community
#>
#> Variable N Missing % Missing
#> --------- --- ------- ---------
#> Income 103 6 5.8%
#> Education 103 6 5.8%
#> Age 103 0 0.0%
#>
#> Complete cases: 91 of 103 (88.3%)
#> Listwise filter activated -- 12 cases will be excluded from
#> subsequent analyses.
#> jcomplete Preview -- rows scheduled for deletion
#> Row Income Education Age DeletionCheck
#> --- ------ --------- --- -------------
#> 6 4 48 1
#> 16 34000 32 1
#> 27 2 49 1
#> 31 5 31 1
#> 39 27000 40 1
#> 43 1 50 1
#> 55 64000 45 1
#> 61 2 56 1
#> 68 48000 46 1
#> 72 3 47 1
#>
#> Showing the first 10 of 12 dropped rows.
#> (The preview viewer needs an interactive RStudio session; showing the first 10
#> in the console instead.)
jcomplete(preview = TRUE) # Preview the already-set filter (viewer)
#> jcomplete Preview -- rows scheduled for deletion
#> Row Income Education Age DeletionCheck
#> --- ------ --------- --- -------------
#> 6 4 48 1
#> 16 34000 32 1
#> 27 2 49 1
#> 31 5 31 1
#> 39 27000 40 1
#> 43 1 50 1
#> 55 64000 45 1
#> 61 2 56 1
#> 68 48000 46 1
#> 72 3 47 1
#>
#> Showing the first 10 of 12 dropped rows.
#> (The preview viewer needs an interactive RStudio session; showing the first 10
#> in the console instead.)
jcomplete(preview = TRUE, non.deletes = TRUE) # Viewer shows all cases
#> jcomplete Preview -- rows scheduled for deletion
#> Row Income Education Age DeletionCheck
#> --- ------ --------- --- -------------
#> 6 4 48 1
#> 16 34000 32 1
#> 27 2 49 1
#> 31 5 31 1
#> 39 27000 40 1
#> 43 1 50 1
#> 55 64000 45 1
#> 61 2 56 1
#> 68 48000 46 1
#> 72 3 47 1
#>
#> Showing the first 10 of 12 dropped rows.
#> (The preview viewer needs an interactive RStudio session; showing the first 10
#> in the console instead.)
jcomplete(console = 10) # Console only -- first 10 dropped rows
#> jcomplete Preview -- rows scheduled for deletion
#> Row Income Education Age DeletionCheck
#> --- ------ --------- --- -------------
#> 6 4 48 1
#> 16 34000 32 1
#> 27 2 49 1
#> 31 5 31 1
#> 39 27000 40 1
#> 43 1 50 1
#> 55 64000 45 1
#> 61 2 56 1
#> 68 48000 46 1
#> 72 3 47 1
#>
#> Showing the first 10 of 12 dropped rows. Use preview = TRUE to see them all in the viewer.
jcomplete(preview = TRUE, console = 25) # Viewer and console
#> jcomplete Preview -- rows scheduled for deletion
#> Row Income Education Age DeletionCheck
#> --- ------ --------- --- -------------
#> 6 4 48 1
#> 16 34000 32 1
#> 27 2 49 1
#> 31 5 31 1
#> 39 27000 40 1
#> 43 1 50 1
#> 55 64000 45 1
#> 61 2 56 1
#> 68 48000 46 1
#> 72 3 47 1
#> 80 52000 30 1
#> 101 46000 29 1
jcomplete(off) # Deactivate
#> jcomplete deactivated for community.
jcomplete(on) # Reactivate
#> jcomplete reactivated for community: Income, Education, Age
jcomplete(community, off) # Deactivate on a named dataset
#> jcomplete deactivated for community.
jcomplete(community, on) # ... and reactivate it
#> jcomplete reactivated for community: Income, Education, Age
jcomplete() # Check status
#> jcomplete active for community: Income, Education, Age
#> (91 of 103 complete cases)
jcomplete(NULL) # Clear the default dataset's setting
#> jcomplete cleared for community (had: Income, Education, Age).
jcomplete(community, NULL) # Clear a named dataset's setting
#> No jcomplete filter set for community.
#> Nothing to clear.
jcomplete(clear.all = TRUE) # Clear every dataset's setting
#> No jcomplete settings to clear.
# juse(NULL) clears the default dataset -- not normally needed. You'd
# clear a default or registration only to undo a mistake, or -- as in
# this example -- to reset state for testing.
juse(NULL)
#> Default data frame cleared.