Skip to contents

Create a frequency table with chi-square statistic, p-value, cramer's v.

Usage

stat_two_cat(df, grp_var, vars, filter = "")

Arguments

df

A single data.frame

grp_var

A character variable from a data.frame

vars

One or more variables from a data.frame

filter

A character single value (default = ''), that will be the value to filter the variables

Value

A gt table.

Examples



mtcars %>%
 dplyr::mutate(vs = paste0("vs = ",vs)) %>%
 stat_two_cat(grp_var = vs,vars = c(am,cyl))
#> Warning: Supplying `...` without names was deprecated in tidyr 1.0.0.
#>  Please specify a name for each selection.
#>  Did you want `data = -name`?
#>  The deprecated feature was likely used in the relper package.
#>   Please report the issue to the authors.
#> Warning: There were 2 warnings in `dplyr::mutate()`.
#> The first warning was:
#>  In argument: `chi_square = purrr::map(.x = data, .f =
#>   ~stats::chisq.test(.$grp_var, .$value))`.
#> Caused by warning in `stats::chisq.test()`:
#> ! Chi-squared approximation may be incorrect
#>  Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
#> Warning: There was 1 warning in `dplyr::mutate()`.
#>  In argument: `dplyr::across(.fns = ~dplyr::if_else(is.na(.), "-",
#>   as.character(.)))`.
#> Caused by warning:
#> ! Using `across()` without supplying `.cols` was deprecated in dplyr 1.1.0.
#>  Please supply `.cols` instead.
#> <div id="oyxjootliy" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
#>   <style>#oyxjootliy table {
#>   font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
#>   -webkit-font-smoothing: antialiased;
#>   -moz-osx-font-smoothing: grayscale;
#> }
#> 
#> #oyxjootliy thead, #oyxjootliy tbody, #oyxjootliy tfoot, #oyxjootliy tr, #oyxjootliy td, #oyxjootliy th {
#>   border-style: none;
#> }
#> 
#> #oyxjootliy p {
#>   margin: 0;
#>   padding: 0;
#> }
#> 
#> #oyxjootliy .gt_table {
#>   display: table;
#>   border-collapse: collapse;
#>   line-height: normal;
#>   margin-left: auto;
#>   margin-right: auto;
#>   color: #333333;
#>   font-size: 16px;
#>   font-weight: normal;
#>   font-style: normal;
#>   background-color: #FFFFFF;
#>   width: auto;
#>   border-top-style: solid;
#>   border-top-width: 2px;
#>   border-top-color: #A8A8A8;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #A8A8A8;
#>   border-left-style: none;
#>   border-left-width: 2px;
#>   border-left-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_caption {
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#> }
#> 
#> #oyxjootliy .gt_title {
#>   color: #333333;
#>   font-size: 125%;
#>   font-weight: initial;
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-bottom-color: #FFFFFF;
#>   border-bottom-width: 0;
#> }
#> 
#> #oyxjootliy .gt_subtitle {
#>   color: #333333;
#>   font-size: 85%;
#>   font-weight: initial;
#>   padding-top: 3px;
#>   padding-bottom: 5px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-top-color: #FFFFFF;
#>   border-top-width: 0;
#> }
#> 
#> #oyxjootliy .gt_heading {
#>   background-color: #FFFFFF;
#>   text-align: center;
#>   border-bottom-color: #FFFFFF;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_bottom_border {
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_col_headings {
#>   border-top-style: solid;
#>   border-top-width: 2px;
#>   border-top-color: #D3D3D3;
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_col_heading {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: normal;
#>   text-transform: inherit;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#>   vertical-align: bottom;
#>   padding-top: 5px;
#>   padding-bottom: 6px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   overflow-x: hidden;
#> }
#> 
#> #oyxjootliy .gt_column_spanner_outer {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: normal;
#>   text-transform: inherit;
#>   padding-top: 0;
#>   padding-bottom: 0;
#>   padding-left: 4px;
#>   padding-right: 4px;
#> }
#> 
#> #oyxjootliy .gt_column_spanner_outer:first-child {
#>   padding-left: 0;
#> }
#> 
#> #oyxjootliy .gt_column_spanner_outer:last-child {
#>   padding-right: 0;
#> }
#> 
#> #oyxjootliy .gt_column_spanner {
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   vertical-align: bottom;
#>   padding-top: 5px;
#>   padding-bottom: 5px;
#>   overflow-x: hidden;
#>   display: inline-block;
#>   width: 100%;
#> }
#> 
#> #oyxjootliy .gt_spanner_row {
#>   border-bottom-style: hidden;
#> }
#> 
#> #oyxjootliy .gt_group_heading {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   text-transform: inherit;
#>   border-top-style: solid;
#>   border-top-width: 2px;
#>   border-top-color: #D3D3D3;
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#>   vertical-align: middle;
#>   text-align: left;
#> }
#> 
#> #oyxjootliy .gt_empty_group_heading {
#>   padding: 0.5px;
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   border-top-style: solid;
#>   border-top-width: 2px;
#>   border-top-color: #D3D3D3;
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   vertical-align: middle;
#> }
#> 
#> #oyxjootliy .gt_from_md > :first-child {
#>   margin-top: 0;
#> }
#> 
#> #oyxjootliy .gt_from_md > :last-child {
#>   margin-bottom: 0;
#> }
#> 
#> #oyxjootliy .gt_row {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   margin: 10px;
#>   border-top-style: solid;
#>   border-top-width: 1px;
#>   border-top-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#>   vertical-align: middle;
#>   overflow-x: hidden;
#> }
#> 
#> #oyxjootliy .gt_stub {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   text-transform: inherit;
#>   border-right-style: solid;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #oyxjootliy .gt_stub_row_group {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   text-transform: inherit;
#>   border-right-style: solid;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   vertical-align: top;
#> }
#> 
#> #oyxjootliy .gt_row_group_first td {
#>   border-top-width: 2px;
#> }
#> 
#> #oyxjootliy .gt_row_group_first th {
#>   border-top-width: 2px;
#> }
#> 
#> #oyxjootliy .gt_summary_row {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   text-transform: inherit;
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #oyxjootliy .gt_first_summary_row {
#>   border-top-style: solid;
#>   border-top-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_first_summary_row.thick {
#>   border-top-width: 2px;
#> }
#> 
#> #oyxjootliy .gt_last_summary_row {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_grand_summary_row {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   text-transform: inherit;
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #oyxjootliy .gt_first_grand_summary_row {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-top-style: double;
#>   border-top-width: 6px;
#>   border-top-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_last_grand_summary_row_top {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-bottom-style: double;
#>   border-bottom-width: 6px;
#>   border-bottom-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_striped {
#>   background-color: rgba(128, 128, 128, 0.05);
#> }
#> 
#> #oyxjootliy .gt_table_body {
#>   border-top-style: solid;
#>   border-top-width: 2px;
#>   border-top-color: #D3D3D3;
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_footnotes {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 2px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_footnote {
#>   margin: 0px;
#>   font-size: 90%;
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #oyxjootliy .gt_sourcenotes {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 2px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #oyxjootliy .gt_sourcenote {
#>   font-size: 90%;
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #oyxjootliy .gt_left {
#>   text-align: left;
#> }
#> 
#> #oyxjootliy .gt_center {
#>   text-align: center;
#> }
#> 
#> #oyxjootliy .gt_right {
#>   text-align: right;
#>   font-variant-numeric: tabular-nums;
#> }
#> 
#> #oyxjootliy .gt_font_normal {
#>   font-weight: normal;
#> }
#> 
#> #oyxjootliy .gt_font_bold {
#>   font-weight: bold;
#> }
#> 
#> #oyxjootliy .gt_font_italic {
#>   font-style: italic;
#> }
#> 
#> #oyxjootliy .gt_super {
#>   font-size: 65%;
#> }
#> 
#> #oyxjootliy .gt_footnote_marks {
#>   font-size: 75%;
#>   vertical-align: 0.4em;
#>   position: initial;
#> }
#> 
#> #oyxjootliy .gt_asterisk {
#>   font-size: 100%;
#>   vertical-align: 0;
#> }
#> 
#> #oyxjootliy .gt_indent_1 {
#>   text-indent: 5px;
#> }
#> 
#> #oyxjootliy .gt_indent_2 {
#>   text-indent: 10px;
#> }
#> 
#> #oyxjootliy .gt_indent_3 {
#>   text-indent: 15px;
#> }
#> 
#> #oyxjootliy .gt_indent_4 {
#>   text-indent: 20px;
#> }
#> 
#> #oyxjootliy .gt_indent_5 {
#>   text-indent: 25px;
#> }
#> </style>
#>   <table class="gt_table" style="table-layout: fixed;; width: 0px" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
#>   <colgroup>
#>     <col style="width:120px;"/>
#>     <col style="width:70px;"/>
#>     <col style="width:70px;"/>
#>     <col style="width:70px;"/>
#>     <col style="width:100px;"/>
#>     <col style="width:70px;"/>
#>     <col style="width:90px;"/>
#>   </colgroup>
#>   <thead>
#>     <tr class="gt_col_headings">
#>       <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id=""></th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_center" rowspan="1" colspan="1" scope="col" id="Levels">Levels</th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_center" rowspan="1" colspan="1" scope="col" id="vs = 0">vs = 0</th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_center" rowspan="1" colspan="1" scope="col" id="vs = 1">vs = 1</th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_center" rowspan="1" colspan="1" scope="col" id="Statistic">Statistic</th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_center" rowspan="1" colspan="1" scope="col" id="P-value">P-value</th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_center" rowspan="1" colspan="1" scope="col" id="Cramer's V">Cramer's V</th>
#>     </tr>
#>   </thead>
#>   <tbody class="gt_table_body">
#>     <tr><th id="stub_1_1" scope="row" class="gt_row gt_center gt_stub">am</th>
#> <td headers="stub_1_1 value" class="gt_row gt_center">-</td>
#> <td headers="stub_1_1 vs = 0" class="gt_row gt_center">-</td>
#> <td headers="stub_1_1 vs = 1" class="gt_row gt_center">-</td>
#> <td headers="stub_1_1 statistic" class="gt_row gt_center">0.3475</td>
#> <td headers="stub_1_1 p_value" class="gt_row gt_center">0.5555</td>
#> <td headers="stub_1_1 cramers_v" class="gt_row gt_center">0.1042</td></tr>
#>     <tr><th id="stub_1_2" scope="row" class="gt_row gt_center gt_stub"></th>
#> <td headers="stub_1_2 value" class="gt_row gt_center">0</td>
#> <td headers="stub_1_2 vs = 0" class="gt_row gt_center">66.67% (12/18)</td>
#> <td headers="stub_1_2 vs = 1" class="gt_row gt_center">50.00% (7/14)</td>
#> <td headers="stub_1_2 statistic" class="gt_row gt_center">-</td>
#> <td headers="stub_1_2 p_value" class="gt_row gt_center">-</td>
#> <td headers="stub_1_2 cramers_v" class="gt_row gt_center">-</td></tr>
#>     <tr><th id="stub_1_3" scope="row" class="gt_row gt_center gt_stub"></th>
#> <td headers="stub_1_3 value" class="gt_row gt_center">1</td>
#> <td headers="stub_1_3 vs = 0" class="gt_row gt_center">33.33% (6/18)</td>
#> <td headers="stub_1_3 vs = 1" class="gt_row gt_center">50.00% (7/14)</td>
#> <td headers="stub_1_3 statistic" class="gt_row gt_center">-</td>
#> <td headers="stub_1_3 p_value" class="gt_row gt_center">-</td>
#> <td headers="stub_1_3 cramers_v" class="gt_row gt_center">-</td></tr>
#>     <tr><th id="stub_1_4" scope="row" class="gt_row gt_center gt_stub">cyl</th>
#> <td headers="stub_1_4 value" class="gt_row gt_center">-</td>
#> <td headers="stub_1_4 vs = 0" class="gt_row gt_center">-</td>
#> <td headers="stub_1_4 vs = 1" class="gt_row gt_center">-</td>
#> <td headers="stub_1_4 statistic" class="gt_row gt_center">21.3399</td>
#> <td headers="stub_1_4 p_value" class="gt_row gt_center">&lt;0.001</td>
#> <td headers="stub_1_4 cramers_v" class="gt_row gt_center">0.8166</td></tr>
#>     <tr><th id="stub_1_5" scope="row" class="gt_row gt_center gt_stub"></th>
#> <td headers="stub_1_5 value" class="gt_row gt_center">4</td>
#> <td headers="stub_1_5 vs = 0" class="gt_row gt_center">5.56% (1/18)</td>
#> <td headers="stub_1_5 vs = 1" class="gt_row gt_center">71.43% (10/14)</td>
#> <td headers="stub_1_5 statistic" class="gt_row gt_center">-</td>
#> <td headers="stub_1_5 p_value" class="gt_row gt_center">-</td>
#> <td headers="stub_1_5 cramers_v" class="gt_row gt_center">-</td></tr>
#>     <tr><th id="stub_1_6" scope="row" class="gt_row gt_center gt_stub"></th>
#> <td headers="stub_1_6 value" class="gt_row gt_center">6</td>
#> <td headers="stub_1_6 vs = 0" class="gt_row gt_center">16.67% (3/18)</td>
#> <td headers="stub_1_6 vs = 1" class="gt_row gt_center">28.57% (4/14)</td>
#> <td headers="stub_1_6 statistic" class="gt_row gt_center">-</td>
#> <td headers="stub_1_6 p_value" class="gt_row gt_center">-</td>
#> <td headers="stub_1_6 cramers_v" class="gt_row gt_center">-</td></tr>
#>     <tr><th id="stub_1_7" scope="row" class="gt_row gt_center gt_stub"></th>
#> <td headers="stub_1_7 value" class="gt_row gt_center">8</td>
#> <td headers="stub_1_7 vs = 0" class="gt_row gt_center">77.78% (14/18)</td>
#> <td headers="stub_1_7 vs = 1" class="gt_row gt_center">-</td>
#> <td headers="stub_1_7 statistic" class="gt_row gt_center">-</td>
#> <td headers="stub_1_7 p_value" class="gt_row gt_center">-</td>
#> <td headers="stub_1_7 cramers_v" class="gt_row gt_center">-</td></tr>
#>   </tbody>
#>   
#>   
#> </table>
#> </div>