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.
An R variable, calculation or data set.
The paste function by default will add a space as the delimiter between values, for example, x and y:
You can also specify the separator as follows:
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:
paste0(x, ";", y, "-", z)
1. Pasting text in a Calculation
In this example, we wish to create an automatically updating text calculation based on the Coke Zero - Female cell from the preferred cola by gender table below:
Here, we can use paste0 to create this calculation:
paste0(table.Preferred.cola.by.Gender["Coke Zero", "Female"], "% of Females prefer Coke Zero")
Notice here that Displayr 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 a Calculation
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: ":
colnames(tab) = paste("Gender: ",colnames(tab))
3. Pasting rows or columns in a Calculation
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 variables
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):
How to Work with R in Displayr
How to Automatically Update Commentary
How to Change Font Color Based on Data Using R
How to use Dynamic Text Box in Displayr
Article is closed for comments.