Learn R Programming

lares (version 5.2.13)

crosstab: Weighted Cross Tabulation

Description

A cross-tabulation function with output similar to STATA, tidy friendly, with weighting possibility.

Usage

crosstab(
  df,
  ...,
  wt = NULL,
  prow = FALSE,
  pcol = FALSE,
  pall = FALSE,
  decimals = 2,
  rm.na = FALSE,
  total = TRUE,
  order = TRUE
)

Value

data.frame. Result of crossing the variables provided in ... and counting how many observations (rows) fall into each criteria.

Arguments

df

Data.frame.

...

Variables. Dependent and independent variables.

wt

Variable, numeric. Weights.

prow, pcol, pall

Boolean. Calculate percent values for rows, columns, or the whole table, respectively.

decimals

Integer. How many decimals should be returned?

rm.na

Boolean. Remove NA values?

total

Boolean. Return total values column?

order

Boolean. Sort columns and rows by frequencies? Else, will be sorted alphabetically

See Also

Other Exploratory: corr_cross(), corr_var(), df_str(), distr(), freqs(), freqs_df(), freqs_list(), freqs_plot(), lasso_vars(), missingness(), plot_cats(), plot_df(), plot_nums(), tree_var()

Examples

Run this code
data(dft) # Titanic dataset
crosstab(dft, Survived, Pclass, total = FALSE)
# Show values in percentages
crosstab(dft, Pclass, Survived, prow = TRUE)
crosstab(dft, Pclass, Survived, pall = TRUE)
# Weighted by another variable
crosstab(dft, Survived, Pclass, wt = Fare, prow = TRUE)

Run the code above in your browser using DataLab