Learn R Programming

rtables (version 0.3.8)

split_rows_by: Add Rows according to levels of a variable

Description

Add Rows according to levels of a variable

Usage

split_rows_by(
  lyt,
  var,
  labels_var = var,
  split_label = var,
  split_fun = NULL,
  format = NULL,
  nested = TRUE,
  child_labels = c("default", "visible", "hidden"),
  label_pos = "hidden",
  indent_mod = 0L
)

Arguments

lyt

layout object pre-data used for tabulation

var

string, variable name

labels_var

string, name of variable containing labels to be displayed for the values of var

split_label

string. Label string to be associated with the table generated by the split. Not to be confused with labels assigned to each child (which are based on the data and type of split during tabulation).

split_fun

function/NULL. custom splitting function

format

FormatSpec. Format associated with this split. Formats can be declared via strings ("xx.x") or function. In cases such as analyze calls, they can character vectors or lists of functions.

nested

boolean, Add this as a new top-level split (defining a new subtable directly under root). Defaults to FALSE

child_labels

string. One of "default", "visible", "hidden". What should the display behavior be for the labels (ie label rows) of the children of this split. Defaults to "default" which flags the label row as visible only if the child has 0 content rows.

label_pos

character(1). Location the variable label should be displayed, Accepts hidden (default for non-analyze row splits), visible, topleft, and - for analyze splits only - default. For analyze calls, default indicates that the variable should be visible if and only if multiple variables are analyzed at the same level of nesting.

indent_mod

numeric. Modifier for the default indent position for the structure created by this function(subtable, content table, or row) and all of that structure's children. Defaults to 0, which corresponds to the unmodified default behavior.

Value

A PreDataTableLayouts object suitable for passing to further layouting functions, and to build_table.

Examples

Run this code
# NOT RUN {
l <- basic_table() %>%
    split_cols_by("ARM") %>%
    split_rows_by("RACE", split_fun = drop_split_levels) %>%
    analyze("AGE", mean, var_labels = "Age", format = "xx.xx")

build_table(l, DM)


basic_table() %>%
    split_cols_by("ARM") %>%
    split_rows_by("RACE") %>%
    analyze("AGE", mean, var_labels = "Age", format = "xx.xx") %>%
    build_table(DM)


l <- basic_table() %>%
    split_cols_by("ARM") %>%
    split_cols_by("SEX") %>%
    summarize_row_groups(label_fstr = "Overall (N)") %>%
    split_rows_by("RACE", split_label = "Ethnicity", labels_var = "ethn_lab",
                  split_fun = drop_split_levels) %>%
    summarize_row_groups("RACE", label_fstr = "%s (n)") %>%
    analyze("AGE", var_labels = "Age", afun = mean, format = "xx.xx")

l

library(dplyr)
DM2 <- DM %>%
    filter(SEX %in% c("M", "F")) %>%
    mutate(
        SEX = droplevels(SEX),
        gender_lab = c("F" = "Female", "M" = "Male",
                       "U" = "Unknown", "UNDIFFERENTIATED" = "Undifferentiated")[SEX],
        ethn_lab = c(
            "ASIAN" = "Asian",
            "BLACK OR AFRICAN AMERICAN" = "Black or African American",
            "WHITE" = "White",
            "AMERICAN INDIAN OR ALASKA NATIVE" = "American Indian or Alaska Native",
             "MULTIPLE" = "Multiple",
             "NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER" =
                 "Native Hawaiian or Other Pacific Islander",
             "OTHER" = "Other", "UNKNOWN" = "Unknown"
        )[RACE]
    )

build_table(l, DM2)

# }

Run the code above in your browser using DataLab