# NOT RUN {
library(dplyr)
library(freqtables)
data(mtcars)
# --------------------------------------------------------------------------
# One-way frequency table with defaults
# - The default confidence intervals are logit transformed - matching the
# method used by Stata
# --------------------------------------------------------------------------
mtcars %>%
freq_table(am)
# A tibble: 2 x 9
# var cat n n_total percent se t_crit lcl ucl
# <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 am 0 19 32 59.4 8.82 2.04 40.9 75.5
# 2 am 1 13 32 40.6 8.82 2.04 24.5 59.1
# --------------------------------------------------------------------------
# One-way frequency table with arbitrary cconfidence intervals
# - The default behavior of freq_table is to return 95% confidence
# intervals (two-sided). However, this behavior can be adjusted to return
# any alpha level. For example, to return 99% confidence intervals just
# pass 99 to the percent_ci parameter of freq_table as demonstrated below.
# --------------------------------------------------------------------------
mtcars %>%
freq_table(am, percent_ci = 99)
# A tibble: 2 x 9
# var cat n n_total percent se t_crit lcl ucl
# <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 am 0 19 32 59.4 8.82 2.74 34.9 79.9
# 2 am 1 13 32 40.6 8.82 2.74 20.1 65.1
# --------------------------------------------------------------------------
# One-way frequency table with Wald confidence intervals
# Optionally, the ci_type = "wald" argument can be used to calculate Wald
# confidence intervals that match those returned by SAS.
# --------------------------------------------------------------------------
mtcars %>%
freq_table(am, ci_type = "wald")
# A tibble: 2 x 9
# var cat n n_total percent se t_crit lcl ucl
# <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 am 0 19 32 59.4 8.82 2.04 41.4 77.4
# 2 am 1 13 32 40.6 8.82 2.04 22.6 58.6
# --------------------------------------------------------------------------
# One-way frequency table with drop = FALSE (default)
# --------------------------------------------------------------------------
df <- data.frame(
id = c(1, 2, 3, 4),
gender = factor(
# All females
c(1, 1, 1, 1),
levels = c(1, 2),
labels = c("female", "male"))
)
df %>%
freq_table(gender)
# A tibble: 2 x 9
# var cat n n_total percent se t_crit lcl ucl
# <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 gender female 4 4 100 0 3.18 NaN NaN
# 2 gender male 0 4 0 0 3.18 NaN NaN
# --------------------------------------------------------------------------
# One-way frequency table with drop = TRUE
# --------------------------------------------------------------------------
df <- data.frame(
id = factor(rep(1:3, each = 4)),
period = factor(rep(1:4)),
x = factor(c(0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1))
)
# Now, supppose we want to drop period 3 & 4 from our analysis.
# By default, this will give us 0s for period 3 & 4, but we want to drop them.
df <- df %>%
filter(period %in% c(1, 2))
df %>%
freq_table(period)
# A tibble: 4 x 9
# var cat n n_total percent se t_crit lcl ucl
# <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 period 1 3 6 50 22.4 2.57 9.12 90.9
# 2 period 2 3 6 50 22.4 2.57 9.12 90.9
# 3 period 3 0 6 0 0 2.57 NaN NaN
# 4 period 4 0 6 0 0 2.57 NaN NaN
# But, we don't want period 3 & 4 in our frequency table at all. That's
# when we should change drop to TRUE.
df %>%
freq_table(period, drop = TRUE)
# A tibble: 4 x 9
# var cat n n_total percent se t_crit lcl ucl
# <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 period 1 3 6 50 22.4 2.57 9.12 90.9
# 2 period 2 3 6 50 22.4 2.57 9.12 90.9
# --------------------------------------------------------------------------
# Two-way frequency table with defaults
# Output truncated to fit the screen
# --------------------------------------------------------------------------
mtcars %>%
freq_table(am, cyl)
# A tibble: 6 x 17
# row_var row_cat col_var col_cat n n_row n_total percent_total se_total
# <chr> <chr> <chr> <chr> <int> <int> <int> <dbl> <dbl>
# 1 am 0 cyl 4 3 19 32 9.38 5.24
# 2 am 0 cyl 6 4 19 32 12.5 5.94
# 3 am 0 cyl 8 12 19 32 37.5 8.70
# 4 am 1 cyl 4 8 13 32 25 7.78
# 5 am 1 cyl 6 3 13 32 9.38 5.24
# 6 am 1 cyl 8 2 13 32 6.25 4.35
# }
Run the code above in your browser using DataLab