categorical_test_by: Categorical test in a pipe

View source: R/dplyr-like.R

categorical_test_byR Documentation

Categorical test in a pipe

Description

Performs classical categorical tests on two columns of a data frame. Per default, will perform chisq.test or fisher.test on the contingency table created by var1 and var2.

Usage

categorical_test_by(
  .tbl,
  var1,
  var2,
  na.rm = T,
  test_function_generator = NULL,
  ...
)

Arguments

.tbl

A data frame

var1

First column to count by

var2

Second column to count by

na.rm

Shall NA values be removed prior to counting?

test_function_generator

A function receiving the matrix to test and returning a named vector with the test function to use. The default uses fisher.test if one count is 5 or lower, otherwise chisq.test. Test functions must return a value with at least one component named "p.value".

...

Passed on to the test function

Details

Returns a one-line data frame as result and thus plays nicely with for example map_dfr.

Value

A one-row data frame with the columns:

  • "var1,var2": The tested variables

  • "test": Label of the test function (default: fisher or chisq)

  • "p-value": P value

  • "result": List column with full result object (default: htest)

  • "contingency_table": List column with contingency table data frame as return by contingency_table_by

Examples

library(magrittr)
if (requireNamespace("datasets", quietly = TRUE))
{
   mtcars %>% categorical_test_by(cyl >= 6, gear)
}

tidytidbits documentation built on March 18, 2022, 6:10 p.m.