Introduction
This article describes how to go from an unsorted table...
...to a table that is sorted on a particular column (e.g., Male).
Requirements
One of the following:
- A crosstab with one question selected in rows and one selected in columns.
- A summary table with at least two rows and one column.
Method
1. Right click on your table and select Reference name.
2. Copy the table name and click OK.
3. Right click in the Report tree and select Add R Output.
4. In the object inspector, go to Properties > R CODE.
5. Add a line to the code that defines the table as table = table_name
using the table_name as copied from step 2.
6. Add another line to that code table[order(table[,column_name], decreasing = TRUE),]
where column_name is the name of the column you wish to sort by in descending order. Instead of the column name, you can also use the column index (e.g., "1" for the first column, "2" for the second column, etc.)
7. OPTIONAL: to show the table in ascending order replace decreasing = TRUE with decreasing = FALSE.
This example code takes a table called table.Preferred.cola.by.D3.Gender and sorts it in descending order by the Male column:
table = table.Preferred.cola.by.D3.Gender
table = table[order(table[,"Male"], decreasing = TRUE),]
The below example code uses the same table and sorts it in ascending order by the first column containing data (Male column).
table = table.Preferred.cola.by.D3.Gender
table = table[order(table[,1], decreasing = FALSE),]
Next
How to Sort Single Column Summary Tables Using R
How to Export Updatable Text to PowerPoint using R
How to Relabel Rows and Columns in an R Table