Introduction
This article describes some of the use cases of the paste and paste0 functions for concatenating text and/or numbers. I can be used in conjunction with How to Automatically Update Commentary.
Requirements
An R variable, R output, or data set.
Method
The paste function by default will add a space as the delimiter between values. Given:
x="First"
y="Second"
The following code will give you First Second
:
paste(x, y)
You can also specify the separator as follows, which will give you First;Second
:
paste(x, y, sep = ";")
The paste0 function, on the other hand, does not automatically include a separator but allows the flexibility of adding different or no separators. The following code will give you First;Second-First
:
paste0(x, ";", y, "-", x)
1. Pasting text in an R Output to automate commentary in your PPT exports
In this example, we wish to create automatically updating text based on the Coke Zero - Female cell from the preferred cola by gender table below:
Here, we can use paste0 to create this:
paste0(table.Preferred.cola.by.Gender["Coke Zero", "Female"], "% of Females prefer Coke Zero")
Notice here that R has remembered all the decimal places, but we can round this to 0 decimals accordingly:
x = table.Preferred.cola.by.Gender["Coke Zero", "Female"]
paste0(round(x,0), "% of Females prefer Coke Zero")
2. Pasting row or column names in an R Output
You can easily add the same text to the beginning or end of each row/column label. Below we will prefix a table's column names with "Gender: ":
tab = table.Preferred.cola.by.Gender
colnames(tab) = paste("Gender: ",colnames(tab))
3. Pasting rows or columns in an R Output
The paste function can also be used for combining multiple rows or columns. Below we have 3 columns of text responses which we intend to combine into a single output:
To apply this to all rows, we need to wrap it within the apply function and use paste's collapse argument to specify the separator:
apply(colas, 1, paste, collapse=", ")
The 1 tells the function to apply it to all rows, whereas 2 would refer to the columns.
4. Pasting variable categories together
Combining categories from different variables using the paste function is as simple as referencing the names of the variables in an R text variable. Below we will combine gender with age:
paste(`D3 - Gender`,`D1 - Age`)
Once we change Properties > Structure to Nominal, the result will be an interlocked Gender by Age variable:
5. Using paste to remove duplicate cases based on multiple variables
Sometimes when you have stacked data, you may want to filter in unique cases based on a response ID and other variables. To do so you can paste together the variable's values and remove duplicates based on these combinations. For example if you have a data set where each row is a survey response for a respondent, brand, and time of day, you can identify unique respondents for each brand using the following code, which returns ResponseID Brand combinations that are not duplicates of rows above (leaving you with the first instance of each pair in the data set):
!duplicated(paste(ResponseID, Brand))
See Also
How to How to Export Updatable Text to PowerPoint from Q
How to Relabel Rows and Columns in an R Table
How to Change Font Color Based on Data Using R
How to Create a Dynamic Text Box