This article describes how to go from multiple tables with more than one statistic...
...to a single combined table:
Requirements
A Displayr document with a set of tables (on the page or created within the R Calculation) with EITHER:
- The same row dimensions and the same number of statistics when appending columns to a table.
- The same column dimensions and the same number of statistics when appending rows to a table.
- The same row and column dimensions when appending a new statistic/calculation to a table.
Method - Combining tables up-and-down
1. Select the first table and copy the name from General > General > Name.
2. Repeat step 1 for the second table. These names will be used in the code below.
3. Click Calculation > Custom Code from the toolbar.
4. In the R Code editor, add a line to load the abind R package:
library(abind)
6. Add a line that calls the (abind)
function, like the below example. In this, new.table is an arbitrary name for the combined table, table1_name is the name of the first table name copied in step 1, above, and table2_name is the name of the second table from step 2. To combine tables over rows, set along = 1
. (For columns use, along = 2
and to combine matching tables with different statistics use, along = 3
.)
new.table = abind(table.preferences, table.satisfaction, along = 1)
7. Add a line that defines the name of each statistic (if different between the input tables) using the format below, where Label1 is the combined label to use for the first statistic in both tables, and Label2 is for the second statistic.
dimnames(new.table)[[3]] = c("Score","Sample Size")
8. Add a line that references the new.table set in step 6:
new.table
The complete code to generate the example above is:
#call library of functions needed
library(abind)
#combine tables
new.table = abind(table.preferences, table.satisfaction, along = 1)
#name statistics
dimnames(new.table)[[3]] = c("Score","Sample Size")
#call final table
new.table
Method - Adding a new statistic to a table
You can create your own custom calculations to add to a table as well. This can be useful when wanting to annotate a visualization dynamically in a custom way, see How to Modify Significance Notation and Add Other Annotations to Visualizations. For example, if you wanted to circle points in your visualization where the Satisfaction was 4, you'd add a new statistic to the table to flag those in some way. Below, I've included code to add a 1 to the table where Satisfaction is rounded to 4 or more.
#call library of functions needed
library(abind)
#combine tables
new.table = abind(table.preferences, table.satisfaction, along = 1)
#name statistics
dimnames(new.table)[[3]] = c("Score","Sample Size")
#create empty matrix the size of the table for custom calculation
flag.matrix = matrix(NA, nrow=NROW(new.table), ncol=NCOL(new.table))
#add 1s in flag.matrix where Satisfaction Score is 4
#Satisfaction is in 3rd row of new.table so using [3,] on flag.matrix
flag.matrix[3,] = ifelse(round(new.table["Satisfaction",,"Score"],0) >= 4,1,NA)
#combine flag.matrix with the original table
new.table = abind(new.table, flag.matrix, along = 3)
#set name of new flag statistic which is the 3rd statistic
dimnames(new.table)[[3]][3]="Flag"
#call final table
new.table