Evaluate associations between categorical variables

cross_tabs(
  dataset,
  var1,
  var2,
  tab = NULL,
  data_filter = "",
  envir = parent.frame()
)

Arguments

dataset

Dataset (i.e., a data.frame or table)

var1

A categorical variable

var2

A categorical variable

tab

Table with frequencies as alternative to dataset

data_filter

Expression entered in, e.g., Data > View to filter the dataset in Radiant. The expression should be a string (e.g., "price > 10000")

envir

Environment to extract data from

Value

A list of all variables used in cross_tabs as an object of class cross_tabs

Details

See https://radiant-rstats.github.io/docs/basics/cross_tabs.html for an example in Radiant

See also

summary.cross_tabs to summarize results

plot.cross_tabs to plot results

Examples

cross_tabs(newspaper, "Income", "Newspaper") %>% str()
#> List of 9 #> $ elow : int 0 #> $ res : tibble [1 × 4] (S3: tbl_df/tbl/data.frame) #> ..$ statistic: Named num 188 #> .. ..- attr(*, "names")= chr "X-squared" #> ..$ p.value : num 9.69e-43 #> ..$ parameter: int 1 #> ..$ method : chr "Pearson's Chi-squared test" #> $ cst :List of 10 #> ..$ statistic: Named num 188 #> .. ..- attr(*, "names")= chr "X-squared" #> ..$ parameter: Named int 1 #> .. ..- attr(*, "names")= chr "df" #> ..$ p.value : num 9.69e-43 #> ..$ method : chr "Pearson's Chi-squared test" #> ..$ data.name: chr "tab" #> ..$ observed : 'table' num [1:2, 1:2] 83 180 276 41 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "Low Income" "High Income" #> .. .. ..$ : chr [1:2] "WS Journal" "USA Today" #> ..$ expected : num [1:2, 1:2] 163 100 196 121 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "Low Income" "High Income" #> .. .. ..$ : chr [1:2] "WS Journal" "USA Today" #> ..$ residuals: 'table' num [1:2, 1:2] -6.25 7.97 5.7 -7.26 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "Low Income" "High Income" #> .. .. ..$ : chr [1:2] "WS Journal" "USA Today" #> ..$ stdres : 'table' num [1:2, 1:2] -13.7 13.7 13.7 -13.7 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "Low Income" "High Income" #> .. .. ..$ : chr [1:2] "WS Journal" "USA Today" #> ..$ chi_sq : 'table' num [1:2, 1:2] 39.1 63.5 32.4 52.7 #> .. ..- attr(*, "dimnames")=List of 2 #> .. .. ..$ : chr [1:2] "Low Income" "High Income" #> .. .. ..$ : chr [1:2] "WS Journal" "USA Today" #> ..- attr(*, "class")= chr "htest" #> $ not_vary : chr(0) #> $ df_name : chr "newspaper" #> $ var1 : chr "Income" #> $ var2 : chr "Newspaper" #> $ tab : 'table' num [1:2, 1:2] 83 180 276 41 #> ..- attr(*, "dimnames")=List of 2 #> .. ..$ : chr [1:2] "Low Income" "High Income" #> .. ..$ : chr [1:2] "WS Journal" "USA Today" #> $ data_filter: chr "" #> - attr(*, "class")= chr [1:2] "cross_tabs" "list"
table(select(newspaper, Income, Newspaper)) %>% cross_tabs(tab = .)
#> $elow #> [1] 0 #> #> $res #> # A tibble: 1 x 4 #> statistic p.value parameter method #> <dbl> <dbl> <int> <chr> #> 1 188. 9.69e-43 1 Pearson's Chi-squared test #> #> $cst #> #> Pearson's Chi-squared test #> #> data: tab #> X-squared = 187.78, df = 1, p-value < 2.2e-16 #> #> #> $nm #> [1] "Income" "Newspaper" #> #> $df_name #> [1] "." #> #> $dataset #> #> #> $var1 #> [1] "Income" #> #> $var2 #> [1] "Newspaper" #> #> $tab #> Newspaper #> Income WS Journal USA Today #> Low Income 83 276 #> High Income 180 41 #> #> $data_filter #> [1] "" #> #> attr(,"class") #> [1] "cross_tabs" "list"