# Creating a table to sort
# Function that gives two statistics per table-tree "leaf"
more_analysis_fnc <- function(x) {
in_rows(
"median" = median(x),
"mean" = mean(x),
.formats = "xx.x"
)
}
# Main layout of the table
raw_lyt <- basic_table() %>%
split_cols_by("ARM") %>%
split_rows_by(
"RACE",
split_fun = drop_and_remove_levels("WHITE") # dropping WHITE levels
) %>%
summarize_row_groups() %>%
split_rows_by("STRATA1") %>%
summarize_row_groups() %>%
analyze("AGE", afun = more_analysis_fnc)
# Creating the table and pruning empty and NAs
tbl <- build_table(raw_lyt, DM) %>%
prune_table()
# Peek at the table structure to understand how it is built
table_structure(tbl)
# Sorting only ASIAN sub-table, or, in other words, sorting STRATA elements for
# the ASIAN group/row-split. This uses content_table() accessor function as it
# is a "ContentRow". In this case, we also base our sorting only on the second column.
sort_at_path(tbl, c("ASIAN", "STRATA1"), cont_n_onecol(2))
# Custom scoring function that is working on "DataRow"s
scorefun <- function(tt) {
# Here we could use browser()
sum(unlist(row_values(tt))) # Different accessor function
}
# Sorting mean and median for all the AGE leaves!
sort_at_path(tbl, c("RACE", "*", "STRATA1", "*", "AGE"), scorefun)
last_cat_scorefun <- function(x, decreasing, lastcat) {
mycat <- obj_name(x)
if (mycat == lastcat) {
ifelse(isTRUE(decreasing), -Inf, Inf)
} else {
match(tolower(substr(mycat, 1, 1)), letters)
}
}
lyt2 <- basic_table() %>%
split_rows_by("SEX") %>%
analyze("AGE")
tbl2 <- build_table(lyt2, DM)
sort_at_path(tbl2, "SEX", last_cat_scorefun, lastcat = "M")
sort_at_path(tbl2, "SEX", last_cat_scorefun, lastcat = "M", decreasing = FALSE)
Run the code above in your browser using DataLab