In this case, we are using r’s paste function to concatenate values into a single string.

Introducing The Collapse parameter

A key limitation in the example above: we assume we’re concatenating a constant number of arguments. This doesn’t always occur in the real world. Suppose we want to write an R function which generates comma delimited text for an unspecified number of arguments. Perhaps we’re working on the (always popular) custom XML format for passing data into a web application’s API. Or unpacking an unusual format from an old database. In any event, we may wish to write our process to accept information as a vector of unspecified length. Repeating the example above as a vector…

An efficient conversion of vector to text, but we’ve retained the fragmented nature of the original data. A small change is needed, to string these elements together. We shall specify a “collapse” parameter to concatenate the results into a single string. As shown below:

Concatenating multiple sets of strings

These functions can be combined to create a more complicated format. For example, suppose we have a vector of information from a database that we want to convert into a dictionary format.

c("12 NE 1st Street", "New York", "NY", "Donor", "Gold")

We know what each of these values is:

c("address", "city", "state", "status, "tier")

And we would like our final format to be something along the lines of:

key:value,key:value

So we present the paste function with two vectors – the first being field names, the second containing field values. We join them using a colon and join the pieces together using semi-colons. Code is below.

Which is an example of how you can use paste to concatenate strings in R to build complex formats.

Shortcuts – paste vs. paste0 functions

The default value of sep is a blank space. Since reducing a vector to a string without any separating space is a common use case, a shorthand version of the function has been developed. The paste0 function has a zero length space as it’s default value for sep. In effect, using paste0 is the same as declaring:

paste(values, sep="")

For more information about handy functions for cleaning up data, check out our functions reference.

Examples For Common Uses

Resources to help you simplify data collection and analysis using R. Automate all the things!