The method paste() in R enables you to combine strings, numerical values and other data types. It converts all the elements into strings.

What is paste in R used for?

R’s paste() function is used to combine different elements and create strings. The number of arguments it takes can vary, and those arguments can be strings or other data types. It then returns a string that connects the elements with each other. The function allows for con­cat­en­a­tions in both ho­ri­zont­al and vertical form.

The ability to control the format­ting of the resulting string makes paste() par­tic­u­larly useful for text pro­cessing in R. And if you combine it with functions like sprintf() and paste0(), you can perform complex text ma­nip­u­la­tion and present data in a very readable form. This is useful for axis labels for graphics, the format­ting for output reports and com­pil­a­tions of variable names.

What is the basic syntax of paste() in R?

The paste() function in R offers various ways to format strings, including options for working with empty fields and inserting user-defined place­hold­ers.

The basic syntax consists of the following:

paste(x, sep=" ", collapse=NULL)
R
  • x: The parameter x stands for the elements that you want to combine
  • sep: This is where you can define a separator, which will appear between the combined elements. The default is a space.
  • collapse: This parameter is useful for combing the elements from a vector into a single string

Examples of how to use paste() in R

Below we’ll show examples of paste() in R and some of its ap­plic­a­tions in text pro­cessing and data visu­al­isa­tion.

Simple con­cat­en­a­tions

Let’s start with the simplest use of paste() in R – combining elements, without using the sep or collapse para­met­ers. The default values for these para­met­ers will be applied.

result <- paste("Hello", "World")
# result = "Hello World"
R

Here we see that the two elements "Hello" and "World" have been combined into a single string, with a space between them. The default value of sep is a space and that of collapse is NULL. You can enter as many arguments into R’s paste() function as you want and combine them with each other.

How to use the separator parameter

You can use the parameter sep to customise which character serves as a separator.

result <- paste("Apple", "Banana", "Orange", sep = ", ")
# result = "Apple, Banana, Orange"
R

In this example, we combine the strings "Apple", "Banana" and "Orange", with a comma and space between them as a separator. We didn’t define collapse in this example, so the result is a string where the combined elements are only divided by a separator.

How to use collapse

The separator parameter sep doesn’t work as expected when paste() is applied to a vector. This is where collapse comes in. You can use collapse to define the symbol or value that will separate the elements of a vector when they are combined into a single string.

paste(c(0,40,33,15,7,98), collapse = "-")
# result = "0-40-33-15-7-98"
R

In the above example, we set the collapse parameter to -. That means the elements from the vector will be separated by a dash in the resulting string.

Using paste() with both sep and collapse

If you’re working with vectors, you can define both sep and collapse as para­met­ers.

paste(c('a', 'b'), 1:10, sep = '-', collapse = ' and ')
# result = "a-1 and b-2 and a-3 and b-4 and a-5 and b-6 and a-7 and b-8 and a-9 and b-10"
R

The result of the above operation is a string in which the elements from the first vector ('a' 'b') are combined with the elements of the second vector (numbers 1-10).

Tip

In our guide, you can also read about other R functions for text ma­nip­u­la­tion, such as R substring() and R gsub() and sub().

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included
Go to Main Menu