When working with tables in R code, it's important to understand how things seen on the table translate into R. When you have multiple statistics in the Statistics > Cells of a table, like in the picture below, the table structure will no longer be a basic matrix but rather an array. This essentially means there's a copy of the table for each statistic (i.e. Column % and Count) and those are grouped together. This all just means that you will need to specify which statistic on the table you'd like to extract when you subset it. This article describes how to go from a multiple-column table with multiple statistics in the Cells...
...to returning only the cells you need.
Requirements
A multiple-column table with multiple statistics in the Statistics > Cells of the table.
Method 1 - Point and click
1. Click on Calculation > Custom Code either via the right-click menu in the Report tree or toolbar.
2. Click on the page to insert a calculation box.
3. While the cursor is blinking (active) in the R Code editor, click on a part of a table that you'd like to extract.
See Bespoke Analyses for examples and a demo of the feature.
4. OPTIONAL: Add a percentage sign or adjust the number of decimal places via the Object Inspector > Appearance > Appearance.
Method 2 - By name
1. Select your table.
2. Copy the name from Object Inspector > General > General > Name.
3. Select Calculation > Custom Code.
4. Go to the R Code editor.
5. Add a line in the format of table_name[row_name, column_name, statistic_name]
Using our example, the code to return just the percentage from the second row of the Male column is:
table.age.by.gender["25 to 29","Male","Column %"]
To instead return percentages from the first three rows of the Male column is:
table.age.by.gender[c("18 to 24","25 to 29","30 to 34"),"Male","Column %"]
Leaving one of these arguments empty returns all rows, columns, or statistics from the table, respectively.
Below, the column argument has been removed from above (table.age.gender[c("18 to 24","25 to 29","30 to 34"),,"Column %"]) so it returns both columns:
6. OPTIONAL: Add a percentage sign or adjust the number of decimal places via Object Inspector > Appearance > Appearance.
Method 3 - By index
1. Select your table.
2. Copy the name from Object Inspector > General > General > Name.
3. Select Calculation > Custom Code.
4. Go to the R Code editor.
5. Add a line in the format of table_name[row_index, column_index, statistic_index]
Using our example, the code to return the percentage from the second row of the first column is: table.age.by.gender[2,1,1]
To instead return percentages from the first three rows of the first column is: table.age.by.gender[c(1,2,3),1,1]
Alternatively, you can use index ranges: table.age.by.gender[1:3,1,1]
You can even reverse this to remove the other rows instead: table.age.by.gender[c(-4:-10,1,1)]
You can additionally remove the last row by using the NROW argument:
table.age.by.gender[-NROW(table.age.by.gender),,]
6. OPTIONAL: Add a percentage sign or adjust the number of decimal places via Object Inspector > Appearance > Appearance.
Next
How to Extract Data from a Single Column Summary Table
How to Extract Data from a Multiple Column Table
How to Extract Data from a Multiple Column Table with Nested Data
UPCOMING WEBINAR: The Roadmap for Market Researchers in the Age of AI