This article describes how to go from a crosstab with averages on the bottom to a table that includes the averages in the column labels:
Requirements
A crosstab with a statistic on the bottom that you want to include in each column header. Neither of the Row or Column variable sets can be grids.
Method
1. Select your table.
2. Go to Properties > RULES on the object inspector.
3. Click the Plus (+) button.
4. Click New custom rule (write your own JavaScript) > Edit JavaScript.
5. Paste the below into the dialog:
form.setHeading("Add table statistic to column labels");
form.setSummary("Add table statistic to column labels");
includeWeb('Table JavaScript Utility Functions');
// Set parameters and text
var statistic_to_add = "Average";
var decimals = 2;
var text_before = "Average =";
// Get labels and values
var vals = below_table.get(statistic_to_add);
var col_labs = table.columnLabels;
var new_labs = [];
// Loop through each column
for (var col = 0; col < table.numberColumns; col++) {
// Create string with old column label and value of specified statistic
var val = parseFloat(vals[0][col]);
var old_lab = col_labs[col];
var new_lab = old_lab + "\n (" + text_before + " " + val.toFixed(decimals) + ")"
new_labs[col] = new_lab;
}
// Set new column labels
table.columnLabels = new_labs;
- In this code, we first define the statistic to use and the format of the text to be appended to each column label.
- Next, we loop through each column and extract the relevant value.
- Each iteration of the loop then appends "(Average = value)" to the existing column label after a line break.
6. OPTIONAL: Change statistic_to_add to use a different statistic other than Average.
7. OPTIONAL: Change decimals to include more or less decimal places.
8. OPTIONAL: Change text_before to adjust the text to append to the row label.
9. OPTIONAL: Go to Inputs > STATISTICS > Below and untick Average.
10. Press the blue Play button > OK > OK.