Tables - Multiway Table

From Q
Jump to navigation Jump to search


Creates a table with multiple categorical variables and, optionally, a numeric variable. Similar to a Pivot Table in Excel.

How to Create a Multiway Table

  1. Add the object:
    1. In Displayr: Insert > More > Tables > Multiway Table
    2. In Q: Create > Tables > Multiway Table
  2. Select the variables to use as the rows in Inputs > Rows.
  3. Optionally, use Inputs > Columns to specify the variables to use as the columns of the table.
  4. Selet the optional numeric variable with Inputs > Numeric variable.

Example

The table below uses Occupation and Work Status as the rows, with Gender as the columns, and Average monthly bill as the numeric variable.

Options

The options in the Object Inspector are organized into two tabs: Inputs and Properties.

Inputs

Rows The variables to appear in the rows, as categories.

Columns (Optional) The variables to appear in the columns, as categories.

Numeric variable (Optional) A variable to analyzing in the cells of the table.

Statistic The statistic to compute (e.g., mean, sum), when specifying both Columns and Numeric variable.

Mean
Minimum
Maximum
Sum

Hide empty rows Removes rows from the table that contain no data.

Hide empty columns Removes columns from the table that contain no data.

Filter The data is automatically filtered using any filters prior to estimating the model.

Weight Where a weight has been set for the R Output, the calibrated weight is used. See Weights in R.

Properties

This tab contains options for formatting the size of the object, as well as the underlying R code used to create the visualization, and the JavaScript code use to customize the Object Inspector itself (see Object Inspector for more details about these options). Additional options are available by editing the code.

More Information

Creating tables with multiple variables (filters and multiway tables)

Code

var heading_text = 'Multiway Table';
if (!!form.setObjectInspectorTitle)
    form.setObjectInspectorTitle(heading_text);
else 
    form.setHeading(heading_text);

form.dropBox({label: "Rows", 
            types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"], 
            name: "formRows",
            multi:true});
var columns = form.dropBox({label: "Columns",
            types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"], 
            name: "formColumns",
            multi: true, 
            required: false});
var numeric = form.dropBox({label: "Numeric",
            types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"], 
            name: "formNumeric",
            multi: false, 
            required: false});
if (!!numeric.getValue()) {
    let allowed_stats = ["Mean", "Minimum", "Maximum", "Sum"];
    if (columns.getValues() == null || columns.getValues().length == 0) {
	allowed_stats.unshift("Count");
        form.listBox({label: "Statistic",
            alternatives: allowed_stats,
            initialValues: allowed_stats,
            multiSelection: true,
            required: true,
            name: "formStatisticLB",
            default_value: "Mean"});
    }else {
        form.comboBox({label: "Statistic",
            alternatives: allowed_stats,
            name: "formStatistic",
            default_value: "Mean"});
    }
}
form.checkBox({label: "Hide empty rows", name: "formHideRows", default_value: true});
form.checkBox({label: "Hide empty columns", name: "formHideColumns", default_value: true});
library(flipStatistics)
stats <- get0("formStatisticLB")
if (is.null(stats)) {
    stats <- get0("formStatistic", ifnotfound = "Mean")
}else {
    stats <- names(stats)[stats]
}

multiway <- Multiway(formRows,
    if (length(formColumns) == 0) NULL else formColumns,
    if (length(formNumeric) == 1) NULL else formNumeric,
    numeric.statistics = stats,
    hide.empty.rows = formHideRows,
    hide.empty.columns = formHideColumns,
    subset = QFilter,
    weights = QPopulationWeight)