Show Each Statistic in a Separate Column

From Q
Jump to: navigation, search

Sometimes it is desirable for statistics to be shown in separate columns rather than above one another in each cell of the table. The script below shifts the table around so that this is achieved.

This example applies the script to a crosstab between a question about Income and a banner that contains Age and Gender. The table is showing the Column % and n statistics.

Before:

StatisticsInSeparateColumnsBefore.PNG

After:

StatisticsInSeparateColumnsAfter.PNG

The two statistics are shown side-by-side.

Caveats

This script stores all of the statistics from the original table in the first statistic from that table. In the example above, all statistics have been stored in the Column %. For this reason, you should turn off the percent signs when you use this script because a percent sign will be shown in each cell regardless of which statistic is shown in that column.

As for other scripts that substantially modify the table, the significance results are no longer applicable and should be turned off. See Caveats for Table JavaScript for more details.

It is best to make any adjustments to the question in the brown drop-down from a separate table.

The Script

if (table.numberColumns > 1 && table.statistics.length > 1) { // This table is two-dimensional and contains multiple statistics
 
	// Store the initial state of the table
 
	var initial_num_cols = table.numberColumns;
	var initial_col_labels = table.columnLabels;
	var initial_col_spans = table.columnSpans;
	var initial_statistics = table.statistics;
	var num_stats = initial_statistics.length
 
	// Grab the stats and form new labels for the columns
	var initial_stat_values = [];
	for (var j = 0; j < num_stats; j++) {
		initial_stat_values[j] = table.get(initial_statistics[j]);
	}
 
	// Set the table to display a single statistic
	table.statistics = [initial_statistics[0]];
 
	// Create new column labels
	var new_col_labels = [];
	for (var j = 0; j < initial_num_cols; j ++) {
		new_col_labels = new_col_labels.concat(initial_statistics);
	}
 
	var new_num_cols = new_col_labels.length;
 
	// Expand the table
	while(table.numberColumns < new_num_cols) table.insertColumnAfter(table.numberColumns-1, "");
 
	// Build new statistics table from original statistics
	var new_stats = table.get(table.statistics[0]);
	for (var j = 0; j < initial_num_cols; j++) {
		for (var k = 0; k < num_stats; k++) {
			var cur_stats = initial_stat_values[k]
			for (var row = 0; row < table.numberRows; row++) {
				new_stats[row][(j*num_stats + k)] = cur_stats[row][j];
			}
		}
	}
	table.columnLabels = new_col_labels;
	table.set(initial_statistics[0],new_stats);
	table.clearColumnSpans();
 
	// Recombine the column labels as spans
	// First the column labels
	for (var j = 0; j < initial_col_labels.length; j++) {
		var start = j*num_stats;
		var end = start + num_stats;
		var index_array = [];
		for (var k = start; k < end; k++) {
			index_array.push(k);
		}
		table.spanColumns(index_array,initial_col_labels[j]);
	}
 
    	// Now include all of the original spans
 
	for (var j = 0; j < initial_col_spans.length; j++) {
		var cur_inds = initial_col_spans[j].indices;
        	var start = cur_inds[0]*num_stats;
		var end = cur_inds[cur_inds.length-1]*num_stats + num_stats;
		var index_array = [];
        	for (var k = start; k < end; k++) {
			index_array.push(k);
		}
		table.spanColumns(index_array,initial_col_spans[j].label);
	}
 
}

See also

Personal tools
Namespaces

Variants
Actions
Navigation
Categories
Toolbox