# NOT RUN {
# A simple cross-table:
tab(forcats::gss_cat, marital, race)
# With more variables provided, `tab` makes a subtables for each combination of levels:
# }
# NOT RUN {
tab(forcats::gss_cat, marital, tab_vars = c(year, race))
# }
# NOT RUN {
# You can also add supplementary columns, text or numeric:
# }
# NOT RUN {
tab(dplyr::storms, category, status, sup_cols = c("pressure", "wind"))
# }
# NOT RUN {
# Colors to help the user read the table:
data <- forcats::gss_cat %>%
dplyr::filter(year %in% c(2000, 2006, 2012), !marital %in% c("No answer", "Widowed"))
gss <- "Source: General social survey 2000-2014"
gss2 <- "Source: General social survey 2000, 2006 and 2012"
# Differences between the cell and it's subtable's total cell:
# }
# NOT RUN {
tab(data, race, marital, year, subtext = gss2, pct = "row", color = "diff")
# }
# NOT RUN {
# Differences between the cell and the whole table's general total cell:
# }
# NOT RUN {
tab(data, race, marital, year, subtext = gss2, pct = "row", color = "diff",
comp = "all")
# }
# NOT RUN {
# Historical differences:
# }
# NOT RUN {
data2 <- data %>% dplyr::mutate(year = as.factor(year))
tab(data2, year, marital, race, subtext = gss2, pct = "row",
color = "diff", diff = "first", tot = "col")
# Differences with the total, except if their confidences intervals are superior to them:
tab(forcats::gss_cat, race, marital, subtext = gss, pct = "row", color = "diff_ci")
# Same differences, minus their confidence intervals:
tab(forcats::gss_cat, race, marital, subtext = gss, pct = "row", color = "after_ci")
# Contribution of cells to table's variance, like in a correspondence analysis:
tab(forcats::gss_cat, race, marital, subtext = gss, color = "contrib")
# }
# NOT RUN {
# Since the result is a tibble, you can use all dplyr verbs to modify it :
# }
# NOT RUN {
library(dplyr)
tab(dplyr::storms, category, status, sup_cols = c("pressure", "wind")) %>%
dplyr::filter(category != "-1") %>%
dplyr::select(-`tropical depression`) %>%
dplyr::arrange(is_totrow(.), desc(category))
# }
# NOT RUN {
# }
# NOT RUN {
# With `dplyr::arrange`, don't forget to keep the order of tab variables and total rows:
tab(data, race, marital, year, pct = "row") %>%
dplyr::arrange(year, is_totrow(.), desc(Married))
# }
Run the code above in your browser using DataLab