This article describes some of the use cases of the paste and paste0 functions for concatenating text and/or numbers. It can be used in conjunction with How to Automatically Update Commentary.
1. Pasting text in a Calculation
2. Pasting row or column names in a Calculation
3. Pasting rows or columns in a Calculation
5. Using paste to create a unique identifier
Requirements
An R variable, calculation, or data set.
Method
The paste function by default will add a space as the delimiter between values, for example, x and y:
paste(x, y)
You can also specify the separator as follows using sep:
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)
You can also join together multiple strings into one string using collapse:
#combine letters
paste(c("a","c","e"), c("b","d","f"), sep = ",")
#above gives you 3 separate strings: c("a,b", "c,d", "e,f")
paste(c("a","c","e"), c("b","d","f"), sep = ",", collapse=",")
#above gives you 1 string using collapse: "a,b,c,d,e,f"
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 create a unique identifier
Sometimes you may need to create your own unique identifier in a file based on responses from multiple variables. This is useful when merging together files for specific response IDs + another variable, and when you have stacked data and need to edit the raw data (Displayr requires a unique id) or identify unique combinations of cases (such as people who purchased something within each month).
To do so you can paste together the variable's values. You may also want to add in a separator to delineate the values. 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 create a unique identifier variable by:
paste(ResponseID,"-", Brand, "-", Time)
You can also identify unique values of combinations of variables. Using the above example, you can identify unique respondents for each brand using the following code, which returns ResponseID Brand combinations that are not duplicates of rows above (identifying the first instance of each pair in the data set):
!duplicated(paste(ResponseID,"-", Brand))
Next
How to Work with R in Displayr
How to Automatically Update Commentary