tab {sumvar}R Documentation

Create a cross-tabulation of two categorial variables

Description

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)
summary <- tab(example_data, group1, group2) # Save summary statistics as a tibble.

[Package sumvar version 0.1 Index]