Skip to contents

Creates a "n x n" cross-tabulation of two categorical variables, with row percentages. Includes options for adding frequentist hypothesis testing.

The function accepts an input from a dplyr pipe "%>%" and outputs the results as a tibble.

eg. example_data %>% tab(variable1, variable2)

Usage

tab(data, variable1, variable2, test = "none")

Arguments

data

The data frame or tibble

variable1

The first categorical variable

variable2

The second categorical variable

test

Optional frequentist hypothesis test, use test=exact for Fisher's exact or test=chi for Chi squared

Value

A tibble with a cross-tabulation of frequencies and row percentages

Examples

example_data <- dplyr::tibble(id = 1:100, group1 = sample(c("a", "b", "c", "d"),
                                                  size = 100, replace = TRUE),
                                                  group2= sample(c("male", "female"),
                                                  size = 100, replace = TRUE))
example_data$group1[sample(1:100, size = 10)] <- NA  # Replace 10 with missing
tab(example_data, group1, group2)
#> # A tibble: 5 × 5
#>   group1 N_female N_male Percent_female Percent_male
#>   <chr>     <int>  <int>          <dbl>        <dbl>
#> 1 a             9     11           45           55  
#> 2 b             7     15           31.8         68.2
#> 3 c            15     16           48.4         51.6
#> 4 d            13      4           76.5         23.5
#> 5 NA            5      5           50           50  
summary <- tab(example_data, group1, group2) # Save summary statistics as a tibble.