This article describes how to extract statistics from a table using a Rule that can then be used for further calculations.
Requirements
- This article assumes you are already in the New custom rule (write your own JavaScript) > Edit JavaScript Rule view specified in How to Create a Custom Rule and have read this article.
Please note these steps require a Displayr license.
Method
Referencing statistics
Displayr differentiates between statistics in cells, to the right and below a table. You can see these in the object inspector under Data > Statistics > Cells/Right/Below. In Table JavaScript these are referenced separately as follows:
table.statistics
right_table.statistics
below_table.statistics
In each case, only the statistics that are currently being displayed in a table will be included.
If you wish to return a list of all statistics that can be computed from this table, you can instead append the availableStatistics
property to the appropriate table statistic location:
table.availableStatistics
right_table.availableStatistics
below_table.availableStatistics
Note, not all tables will have each of these types of statistics:
- A Nominal, Ordinal, Binary - Multi, Binary - Multi (Compact), or Number - Multi summary table will only have cell and below statistics.
- A Nominal - Multi or Ordinal - Multi summary table will only have cell and right statistics.
- A crosstab, Binary - Grid, or Number - Grid will include cell, right, and below statistics.
- When you switch rows and columns, the right and below statistics will be reversed.
You can check whether right or below statistics exist by using the below functions which will return true or false:
belowTableExists()
rightTableExists()
Note, you must include includeWeb('Table JavaScript Utility Functions');
at the top of the Rule in this case, as the underlying code for these functions only exists in this online library.
Referencing values from a statistic
You can return the values of a statistic by using the get
function:
var cell_vals = table.get("%");
var right_vals = right_table.get("Sum");
var below_vals = below_table.get("Average");
In this example, cell_vals
will, for example, "get" the statistic values for every cell in the table across all rows and columns.
One thing to note here is that this function uses the original statistic name, that is the name from Displayr's sister product, Q. To avoid having to know the Q statistic name when different, you can instead use the Table JavaScript Utility function getOriginalName
. In the below example, we return the original name for Column Sample Size (which is Column n) and pass this instead into the get
function:
var stat = getOriginalName("Column Sample Size");
var below_vals = below_table.get(stat);
If you want to have a more flexible Rule that uses the primary statistic, you can combine the get
function with the statistics
property:
table.get(table.statistics[0])
The format of the get
function is a two-dimensional array, where the rows are in the first index and the columns in the second index. So an array of cell statistics in a table might look like this:
[[col1-row1, col2-row1, ...],[col1-row2, col2-row2, ...]]
Using thearray[row index][column index]
reference format, vals[0][0]
in this case, would be the first table cell, that is col1-row1 for the array.
Note that for one-dimensional SUMMARY tables, a two-dimensional array will still be returned, with only one column in the second dimension. The value type will be based on whether the returning statistic is text or numeric.
Next
How to Create a Custom Statistic or Calculation via a Rule
Later
How to Modify Table Rows and Columns Using a Rule
How to Modify Table Cells Using a Rule
How to Create User Input Fields in a Rule
How to Add Table Validations to Rules
How to Reference and Calculate Tables in a Rule
How to Add Table Spans Using a Rule
How to Add a Custom Table Footnote