Introduction
This article describes how to go from a crosstab with a nested banner...
...to an R table where the nested span labels are included in the column labels:
By default, only the lower level labels from a nested banner will flow through to an R table.
Requirements
- A crosstab with a nested banner in the columns. If your nested columns are due to a multi or grid variable set selected in the rows of the table, please see How to Extract Data from a Multiple Column Table with Nested Data.
- Familiarity of how to reference tables in R, see matrices in How to Work with Data in R.
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. Paste the below under Properties > R CODE:
#identify mytable
mytable = table.D2.Income.by.BANNER1
#create data.frame of the column spans and column names
spans = data.frame(attr(mytable,"span")["columns"])
#combine the span and name separated by -
newlabels = apply(spans,1,paste,collapse=" - ")
#make new column names for table
colnames(mytable) = newlabels
#return final result
mytable
- The above code defines the table using the reference name from step 2.
- Next, it pulls the span labels from the columns in the original table using the attr command.
- We can then use the apply function to add the span labels to the column labels using a dash as the separator.
- Finally, we return the table and add these new column labels to it.
Note, if you have spans in your table rows instead, you should reference rownames instead of colnames in your code and amend the span reference code to rows. Full code below:
#identify mytable
mytable = table.D2.Income.by.BANNER1
#create data.frame of the row spans and row names
spans = data.frame(attr(mytable,"span")["rows"])
#combine the span and name separated by -
newlabels = apply(spans,1,paste,collapse=" - ")
#make new row names for table
rownames(mytable) = newlabels
#return final result
mytable
Next
How to Extract Data from a Multiple Column Table with Nested Data
How to Extract and Modify Attributes of a table using R
How to Combine Tables with Multiple Statistics Using R
How to Extract Information from an R Object