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.
Please note this requires the Data Stories module or a Displayr license.
Method
1. Select your table.
2. Copy the name from General > GENERAL > Name.
3. Go to Calculation > Custom Code.
4. Paste the below under Data > 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 the below:
spans = attr(mytable,"span")["rows"]
See Also
How to Extract Data from a Multiple Column Table with Nested Data
How to Extract Data from a Multiple Column Table
How to Extract Data from a Multiple Column Table with Multiple Statistics
How to Work with R in Displayr