split_funcs {rtables} | R Documentation |
Split functions
Description
This is a collection of useful, default split function that can help you in dividing the data, hence the
table rows or columns, into different parts or groups (splits). You can also create your own split function if you
need to create a custom division as specific as you need. Please consider reading custom_split_funs if
this is the case. Beyond this list of functions, you can also use add_overall_level()
and add_combo_levels()
for adding or modifying levels and trim_levels_to_map()
to provide possible level combinations to filter the split
with.
Usage
keep_split_levels(only, reorder = TRUE)
remove_split_levels(excl)
drop_split_levels(df, spl, vals = NULL, labels = NULL, trim = FALSE)
drop_and_remove_levels(excl)
reorder_split_levels(neworder, newlabels = neworder, drlevels = TRUE)
trim_levels_in_group(innervar, drop_outlevs = TRUE)
Arguments
only |
( |
reorder |
( |
excl |
( |
df |
( |
spl |
( |
vals |
( |
labels |
( |
trim |
( |
neworder |
( |
newlabels |
( |
drlevels |
( |
innervar |
( |
drop_outlevs |
( |
Value
A function that can be used to split the data accordingly. The actual function signature is similar to the one you can define when creating a fully custom one. For more details see custom_split_funs.
Functions
-
keep_split_levels()
: keeps only specified levels (only
) in the split variable. If any of the specified levels is not present, an error is returned.reorder = TRUE
(the default) orders the split levels according to the order ofonly
. -
remove_split_levels()
: Removes specified levels (excl
) from the split variable. Nothing done if not in data. -
drop_split_levels()
: Drops levels that have no representation in the data. -
drop_and_remove_levels()
: Removes specified levelsexcl
and drops all levels that are not in the data. -
reorder_split_levels()
: Reorders split levels followingneworder
, which needs to be of same size as the levels in data. -
trim_levels_in_group()
: Takes the split groups and removes levels ofinnervar
if not present in those split groups. If you want to specify a filter of possible combinations, please consider usingtrim_levels_to_map()
.
Note
The following parameters are also documented here but they are only the default
signature of a split function: df
(data to be split), spl
(split object), and vals = NULL
,
labels = NULL
, trim = FALSE
(last three only for internal use). See custom_split_funs for more details
and make_split_fun()
for a more advanced API.
See Also
custom_split_funs, add_overall_level()
, add_combo_levels()
, and trim_levels_to_map()
.
Examples
# keep_split_levels keeps specified levels (reorder = TRUE by default)
lyt <- basic_table() %>%
split_rows_by("COUNTRY",
split_fun = keep_split_levels(c("USA", "CAN", "BRA"))
) %>%
analyze("AGE")
tbl <- build_table(lyt, DM)
tbl
# remove_split_levels removes specified split levels
lyt <- basic_table() %>%
split_rows_by("COUNTRY",
split_fun = remove_split_levels(c(
"USA", "CAN",
"CHE", "BRA"
))
) %>%
analyze("AGE")
tbl <- build_table(lyt, DM)
tbl
# drop_split_levels drops levels that are not present in the data
lyt <- basic_table() %>%
split_rows_by("SEX", split_fun = drop_split_levels) %>%
analyze("AGE")
tbl <- build_table(lyt, DM)
tbl
# Removing "M" and "U" directly, then "UNDIFFERENTIATED" because not in data
lyt <- basic_table() %>%
split_rows_by("SEX", split_fun = drop_and_remove_levels(c("M", "U"))) %>%
analyze("AGE")
tbl <- build_table(lyt, DM)
tbl
# Reordering levels in split variable
lyt <- basic_table() %>%
split_rows_by(
"SEX",
split_fun = reorder_split_levels(
neworder = c("U", "F"),
newlabels = c(U = "Uu", `F` = "Female")
)
) %>%
analyze("AGE")
tbl <- build_table(lyt, DM)
tbl
# Reordering levels in split variable but keeping all the levels
lyt <- basic_table() %>%
split_rows_by(
"SEX",
split_fun = reorder_split_levels(
neworder = c("U", "F"),
newlabels = c("Uu", "Female"),
drlevels = FALSE
)
) %>%
analyze("AGE")
tbl <- build_table(lyt, DM)
tbl
# trim_levels_in_group() trims levels within each group defined by the split variable
dat <- data.frame(
col1 = factor(c("A", "B", "C"), levels = c("A", "B", "C", "N")),
col2 = factor(c("a", "b", "c"), levels = c("a", "b", "c", "x"))
) # N is removed if drop_outlevs = TRUE, x is removed always
tbl <- basic_table() %>%
split_rows_by("col1", split_fun = trim_levels_in_group("col2")) %>%
analyze("col2") %>%
build_table(dat)
tbl