This article describes how to change the header names in a table from this...
... to a table with these names:
Requirements
- An R table that has been generated in an R Output and contains rows and columns
Method 1 - Specify all labels
1. Select your R table.
2. In the object inspector, go to Properties > R CODE.
3. To update the table's column names, add a line to the code like this, where table_name is replaced by the name of your R table:
colnames(table_name) = c("label1", "label2", "label3")
4. To update the table's row names add a line to the code like this, where table_name is replaced by the name of your R table:
rownames(table_name) = c("label1", "label2", "label3")
Note, if your table contains a single column with no header, you should use names instead of rownames.
5. Add a line with the table_name so the updated table is returned, where table_name is replaced by the name of your R table.
Below is the code needed to change the column names in the above example (the name of the input table is merged):
colnames(merged) = c("Awareness","Preference","Affinity")
merged
Method 2 - Replace specific labels
1. Select your R table.
2. In the object inspector, go to Properties > R CODE.
3. To update a specific row label in your table, add a line to the code, where table_name is replaced by the name of your R table:
rownames(table_name)[rownames(table_name) == "old label"] = "new label"
4. To update a specific column label in your table, add a line to the code, where table_name is replaced by the name of your R table:
colnames(table_name)[colnames(table_name) == "old label"] = "new label"
Below is the code for changing the first column name in this example:
colnames(merged)[colnames(merged) == "Q1b - Awareness"] = "Awareness"
Method 3 - Replace all labels based on another table
1. Select the table you wish to copy the labels from.
2. Copy the name from Properties > GENERAL > Name.
3. Select the R table you wish to update.
4. In the object inspector, go to Properties > R CODE.
5. To update all the table's column names with that of the table from steps 1 and 2, add a line to the code:
colnames(table_name) = colnames(reference_table_name)
The equivalent for changing row names is:
rownames(table_name) = rownames(reference_table_name)
Note, the above requires the same number of rows/columns in both tables.
Method 4 - Add a prefix or suffix to labels
1. Select your R table.
2. In the object inspector, go to Properties > R CODE.
3. To add a prefix or suffix to your rows or columns, add the appropriate line to the code below:
rownames(table_name) = paste("Prefix",rownames(table_name),"Suffix")
colnames(table_name) = paste("Prefix",colnames(table_name),"Suffix")
For example: below we will prefix a table's column names with "Gender: ":
colnames(tab) = paste("Gender: ",colnames(tab))
Next
How to Reference Different Items in Your Project in R
How to Use Paste Functions to Create Automatic Text Using R