When To Use the Paste Function in R
Need to concatenate a group of strings? Then you’re in luck. R has a paste function which can be used to assemble strings into a larger string.
What Does The Paste Function Do in R?
The paste function is designed to accept either a string of text entries or a vector and collapse it into a string, pasting the elements next to each other. It has the capability of using a “joining” text string for formatting purposes.
In this sense, it is effectively similar to the “”.join() function in Python or the many equivalents in other programming languages. Or Concatenate() in Excel. You’ve got a vector? Presto, we’ve got a string. Cut and Paste!
Syntax: paste (…, sep = ” “, collapse = NULL)
- Accepts a list of scalar values
- sep – text string to put in between terms for scalar entries
- collapse – used if you are passing a vector the function
Paste in R – Basic Usage
We’re going to dive right in and use the function to assemble some text elements into a single string. This example is based on some silly ideas (pasting names together) but can be used to mass generate variable names and other data manipulation tasks.
# paste in R - basic example > paste("I","AM","Bart","Simpson")  "I AM Bart Simpson" # paste in R - using the sep to format the string > paste("I","AM","Bart","Simpson", sep='!')  "I!AM!Bart!Simpson" # paste in R - dropping a variable into the mix > paste("I","AM",name,"Simpson", sep='!')  "I!AM!Lisa!Simpson" > paste("I","AM",name,"Simpson", sep='!')  "I!AM!Homer!Simpson" # donut?
One of my favorite tricks is to write a helper function for converting the results of a SQL query into appropriate data structures without having to manually adjust code. This can save you a ton of time as a project evolves.
Paste in R – working with vectors
We can take the abstraction up a level and pass the paste function an entire vector of values. If we are doing this, we need to use the collapse option in the paste function to stitch the elements together appropriately.
Note: in the example below, you would probably use a single “blank” character to connect the words. I’m using the “–” for visual effect.
# paste in R example # using collapse to roll up a vector into a string > vocabulary <- c('presto','pompous','pontificate') > paste (vocabulary, collapse='--')  "presto--pompous--pontificate"
Also – this is another great way to knock out variable names if you need to create them on the fly. I’ve written similar code to reformat variable names from SQL queries into other databases or R data sets.