Learn R Programming

descriptr (version 0.6.1)

ds_cross_table: Two way table

Description

Creates two way tables of categorical variables. The tables created can be visualized as bar plots and mosaic plots.

Usage

ds_cross_table(data, var_1, var_2)

# S3 method for ds_cross_table plot(x, stacked = FALSE, proportional = FALSE, print_plot = TRUE, ...)

ds_twoway_table(data, var_1, var_2)

Value

ds_cross_table() returns an object of class "ds_cross_table" containing the contingency table and the utility information used to print it. The cell percentages are stored as proportions (values between 0 and 1) in percent_table, row_percent, column_percent and percent_column. The printed cells show percentages computed from the counts and can differ from the rounded proportions stored in the object in the last decimal place. The number of rows dropped because either variable is missing is stored in utility$missing. ds_twoway_table()

returns a tibble whose percent, row_percent and col_percent columns are also proportions.

Arguments

data

A data.frame or a tibble.

var_1

First categorical variable.

var_2

Second categorical variable.

x

An object of class cross_table.

stacked

If TRUE, the columns of height are portrayed as stacked bars, and if FALSE the columns are portrayed as juxtaposed bars.

proportional

If TRUE, the height of the bars is proportional.

print_plot

logical; if TRUE, prints the plot else returns a plot object.

...

Further arguments to be passed to or from methods.

Examples

Run this code
# cross table
k <- ds_cross_table(mtcarz, cyl, gear)
k

# bar plot
plot(k)

# stacked bar plot
plot(k, stacked = TRUE)

# proportional bar plot
plot(k, proportional = TRUE)

# returns tibble
ds_twoway_table(mtcarz, cyl, gear)

Run the code above in your browser using DataLab