Tables - Multiway Table from Data Set
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
- Add the object:
- In Displayr: Insert > More > Tables > Multiway Table
- In Q: Create > Tables > Multiway Table
- Select the variables to use as the rows in Inputs > Rows.
- Optionally, use Inputs > Columns to specify the variables to use as the columns of the table.
- 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)
How to apply this QScript
- Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
- Click on the QScript when it appears in the QScripts and Rules section of the search results.
OR
- Select Automate > Browse Online Library.
- Select this QScript from the list.
Customizing the QScript
This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.
Customizing QScripts in Q4.11 and more recent versions
- Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
- Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
- Press Edit a Copy (bottom-left corner of the preview).
- Modify the JavaScript (see QScripts for more detail on this).
- Either:
- Run the QScript, by pressing the blue triangle button.
- Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.
Customizing QScripts in older versions
JavaScript
function insertMultiWayTable() {
if (Q.fileFormatVersion() < 20.00) {
let msg = 'Sorry, you must upgrade to a Q version >= 5.12.0 to use this feature.' +
'If you are unable to upgrade, you may instead use: ' +
'\'Create > Tables > Multiway Table\'.';
log(msg);
return false;
}
let displayr = !!Q.isOnTheWeb && Q.isOnTheWeb();
let selected_questions = project.report.selectedQuestions();
let page = project.currentPage();
if (!page) {
if (!displayr)
page = project.report;
else {
page = project.report.appendGroup();
page.name = "New page";
}
}
// If a numeric variable set (not numeric - multi or grid) is present, it is added as
// the numeric variable
// The last selected variable is always placed in the columns
// Remaining variables go in rows
let prefilled_controls = {};
if (selected_questions.length === 1) {
prefilled_controls["formRows"] = selected_questions[0].variables.map(v => v.guid).join(";");
}else if (selected_questions.length > 1) {
let numeric_question = selected_questions.find(q => q.questionType === "Number");
if (numeric_question !== undefined) {
prefilled_controls["formNumeric"] = numeric_question.variables[0].guid;
selected_questions = selected_questions.filter(q => q.name !== numeric_question.name);
}
if (selected_questions.length > 1)
prefilled_controls["formColumns"] = selected_questions.pop().variables.map(v => v.guid).join(";");
prefilled_controls["formRows"] = selected_questions.map(q => q.variables.map(v => v.guid).join(";")).join(";");
}
let table = page.appendStandardR("Tables - Multiway Table", prefilled_controls);
if (Q.fileFormatVersion() > 8.65)
project.report.setSelectedRaw([table]);
return true;
}
if (!insertMultiWayTable())
log("QScript cancelled.");
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.