Allows the creation of hierarchical variable formats with optional "total" and "other" categories.
Usage
new_format(
...,
name = NULL,
var = NULL,
add_total = FALSE,
add_total_overlapping = FALSE,
total_subset_label = "davon",
add_other = FALSE,
add_other_na = FALSE,
code_then_label = TRUE
)Arguments
- ...
One or more variable format entries, which can be:
Character string representing parent of collectively exhaustive subsets (must be followed by a list with subsets).
Two-sided formula specifying
code ~ labelorlabel ~ code. The following shorthand codes are allowed:7for. == 7c(7, 9)for. %in% c(7, 9)7:9for. %in% 7:9
List representing subsets of the previous entry, with an attribute
subsetcontaining logical valuesexhaustiveanddisjoint.
- name
Name for the resulting variable format as character string. If
name = NULL(default) andvaris provided, a name is generated fromvar. Eithernameorvarmust be provided.- var
Variable name to replace the shorthand
.in formulas. Eithernameorvarmust be provided.- add_total
Logical or character. Adds a row with the total for disjoint subsets at the top.
TRUEgenerates a row named "Total"; a string provides a custom label.FALSE(default) adds no row. Mutually exclusive withadd_total_overlapping.- add_total_overlapping
Logical or character. Adds a row with the total for overlapping subsets at the top.
TRUEgenerates a row named "Total"; a string provides a custom label.FALSE(default) adds no row. Mutually exclusive withadd_total.- total_subset_label
Character. Label to be used for subsets of total added by
add_totaloradd_total_overlapping.- add_other
Logical or character. Adds a row at the bottom containing all values not covered by existing codes and not
is.na(var). Requiresvarto be set.TRUEgenerates a row named "Other (non-NA values ofvar)"; a string provides a custom label.FALSE(default) adds no row. Mutually exclusive withadd_other_na.- add_other_na
Logical or character. Adds a row at the bottom containing all values not covered by existing codes (including
NA).TRUEgenerates a row named "Other"; a string provides a custom label.FALSE(default) adds no row. Mutually exclusive withadd_other.- code_then_label
Logical, default
TRUE. Determines whether the formulas in...are interpreted ascode ~ label(TRUE) orlabel ~ code(FALSE).
Value
An object of class procr_varformat, which is a list with two
elements:
formula: thenameas a symbol.data: a list containing a data frame namedname, with structure as documented innew_varformat_data_row()
Details
All parameters must be explicitly named. Not naming a parameter will throw an error. This allows future changes to the parameters and their order without breaking existing code.
For
add_total, the total is calculated not as the sum of all items, but rather as a set union - i.e. all other conditions are combined using|(logical OR). This is slightly less computationally efficient, but works neatly with overlapping subsets.The parameter
add_other/add_other_nais processed beforeadd_total. As a consequence, a computed total will always include "Other" values.This function works similarly to variable preparation and format specification in SAS. However, it additionally allows the specification of hierarchies between individual items.
Examples
new_format(var = Sepal.Length,
"short",
ss_all(. < 5 ~ "< 5",
5 ~ "5"),
"long",
ss_all(. > 5 & . <= 6 ~ "5 - 6",
. > 6 & . <= 6 ~ "6 - 7"),
add_total = TRUE,
add_other = TRUE)
#> Sepal.Length code
#> ╲╴Total Sepal.Length < 5 | Sepal.Length %...
#> ├╴short Sepal.Length < 5 | Sepal.Length %...
#> │ ├╴< 5 Sepal.Length < 5
#> │ └╴5 Sepal.Length %in% 5
#> ├╴long Sepal.Length > 5 & Sepal.Length <...
#> │ ├╴5 - 6 Sepal.Length > 5 & Sepal.Length <= 6
#> │ └╴6 - 7 Sepal.Length > 6 & Sepal.Length <= 6
#> └╴Other (non-NA values of Sepal.Length) !(Sepal.Length < 5 | Sepal.Length...
#>