Introduction
This article describes how to go from a multiple column table with more than one statistic and nested data...
...to returning only the cells you need such as:
Requirements
- A multiple-column table with multiple statistics and nested data created by including a Pick One-Multi question as the blue question used in your table. If you're using a table with a nested banner, please see How to Work with Nested Banners and Spans in R Tables.
- Familiarity of how to reference tables in R, see matrices in How to Work with Data in R.
Method 1 - By name
1. Select your table.
2. Copy the name from Properties > GENERAL > Name.
3. Right click on the Report tree and select Add R Output.
4. Go to Properties > R CODE in the object inspector.
5. To check the table dimensions, write dimnames(table_name)
using the name from step 2 and you will return a list of names for each dimension of your table:
5. Using this definition, we need to add a line in the format of table_name[row_name, column_name, nested_column_name, statistic_name]
Using our example, the code to return just the percentage for females who love Coca-Cola is:
table.gender.brand.attitude["Coca-Cola","Love","Female","Column %"]
To instead return percentages for everyone who likes or loves Coca-Cola is:
table.gender.brand.attitude["Coca-Cola",c("Like","Love"),,"Column %"]
Leaving one of these arguments empty will return all the rows, columns or statistics respectively from the table.
Below the Hate - Love argument has been removed from above so it returns all ratings:
6. OPTIONAL: Add a percentage sign or adjust the number of decimal places via Properties > APPEARANCE > Number format.
Method 2 - By index
1. Select your table
2. Copy the name from Properties > GENERAL > Name
3. Right click on the Report tree and select Add R Output.
4. Go to Properties > R CODE in the object inspector.
5. Using the definition from method 1 step 4, we need to add a line in the format of table_name[row_index, column_index, nested_column_index, statistic_index]
Using our example, the code to return the percentage the percentage for females who love Coca-Cola is: table.gender.brand.attitude[1,5,2,1]
To instead return percentages of everyone who likes or loves Coca-Cola is: table.gender.brand.attitude[1,c(4,5),,1]
Alternatively, you can use index ranges, whereby the above can also be written as: table.gender.brand.attitude[1,c(4:5),,1]
You can even reverse this to remove the other ratings instead: table.gender.brand.attitude[1,c(-1:-3,-6),,1]
You can additionally keep only the last cola category by using the NROW argument:
table.gender.brand.attitude[NROW(table.gender.brand.attitude),,,]
6. OPTIONAL: Add a percentage sign or adjust the number of decimal places via Properties > APPEARANCE > Number format.
See also
How to Work with Nested Banners and Spans in R Tables
How to Extract and Modify Attributes of a table using R