View source: R/quick-summary.R
| tab1 | R Documentation |
Displays formatted frequency tables for one or more categorical variables in the Viewer, with optional Console output.
Data may be supplied explicitly, but when data = NULL the active data set
selected by usedf() is used.
tab1(
...,
by = NULL,
data = NULL,
missing = c("ifany", "no", "always"),
percent = "column",
digits = 1,
overall = TRUE,
drop = TRUE,
show = TRUE,
console = FALSE
)
... |
One or more variables. An explicit data frame may be supplied as the first unnamed argument for backward-compatible R4VN syntax. |
by |
Optional grouping variables supplied as one bare name,
|
data |
Optional data frame. When omitted, active data is used. |
missing |
Missing-value display: |
percent |
Percentage denominator: |
digits |
Decimal places for percentages. |
overall |
Logical; display the unstratified overall distribution. |
drop |
Logical; omit unused factor levels. |
show |
Logical; open the formatted result in the Viewer. Default |
console |
Logical; also print the traditional result in the Console. Default |
by accepts one or more nested stratification variables. For example,
by = c(sex, agegroup) first separates results by sex and then displays
age-group-specific results inside each sex group. Three or more nested
stratification variables are also supported.
Invisibly returns an object of class r4vn_quick.
d <- data.frame(
sex = factor(c("Female", "Male", "Female", "Male", "Female", "Male")),
agegroup = factor(c("<40", "<40", "40+", "40+", "<40", "40+")),
smoking = factor(c("No", "Yes", "No", "Yes", NA, "No")),
vaccinated = factor(c("Yes", "No", "Yes", "Yes", "No", "No"))
)
usedf(d, quiet = TRUE)
tab1(smoking)
tab1(smoking, vaccinated)
tab1(smoking, by = sex)
tab1(smoking, vaccinated, by = c(sex, agegroup))
tab1(smoking, by = c(sex, agegroup), missing = "always")
usedf(clear = TRUE, quiet = TRUE)
# Extended usage examples
d <- data.frame(
sex = factor(c("Female", "Male", "Female", "Male", "Female", "Male")),
agegroup = factor(c("<40", "<40", "40+", "40+", "<40", "40+")),
province = factor(c("HCMC", "HCMC", "HCMC", "Other", "Other", "Other")),
smoking = factor(c("No", "Yes", "No", "Yes", NA, "No")),
vaccinated = factor(c("Yes", "No", "Yes", "Yes", "No", "No"))
)
usedf(d)
# One or several categorical variables
tab1(smoking)
tab1(smoking, vaccinated)
# One level of stratification
tab1(smoking, by = sex)
# Nested stratification: age groups are shown within each sex
tab1(smoking, by = c(sex, agegroup))
# Three nested levels, evaluated in the supplied order
tab1(smoking, vaccinated, by = c(province, sex, agegroup))
# Percentage denominators
tab1(smoking, by = c(sex, agegroup), percent = "column")
tab1(smoking, by = c(sex, agegroup), percent = "row")
tab1(smoking, by = c(sex, agegroup), percent = "cell")
tab1(smoking, by = c(sex, agegroup), percent = "none")
# Missing values and unused factor levels
tab1(smoking, missing = "no")
tab1(smoking, missing = "ifany")
tab1(smoking, missing = "always", drop = FALSE)
# Hide the overall section or retain the result without printing
tab1(smoking, by = sex, overall = FALSE)
result <- tab1(smoking, by = sex, show = FALSE)
result$variables[[1]]$strata
# Explicit data remains supported
tab1(d, smoking, vaccinated, by = c(sex, agegroup))
tab1(smoking, data = d)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.