How To Use The paste() Function In R

paste() function in R

The paste() function in R can convert objects into character vectors and concatenate them. Here is how you can use this ability in your program.

Table of Contents

paste() Function In R

The syntax of this function is quite simple and straightforward:

paste(x1, x2, ... sep, collapse)

In this:

  • x1, x2, and other arguments are R objects that you want to convert to character vectors.
  • The sep argument specifies the string used to separate those arguments. On the other hand, if you want to separate the results, use the collapse arguments. Remember that you can’t use the NA_character_ constant for these two arguments.

This is a simple example of the paste() function:

[1] "1" "2" "3"

The function takes three integer values (1, 2, 3) created by the colon operator (:), converts them into three character values (“1”, “2”, and “3”), and finally, puts them into a single character vector (“1” “2” “3”).

You are free to mix input arguments of different data types. For example, this command takes numeric and character values and converts them into a single character value with paste();

paste("one", 2, "three", 4, 5)
[1] "one 2 three 4 5"

If you want to separate the elements in the output, use the sep argument. Here is how you can put underscores (_) between input arguments with paste():

paste("one", 2, "three", 4, 5, sep = '_')
[1] "one_2_three_4_5"

By default, paste() uses a whitespace character. You can replace it with an empty value to concatenate them into a single string:

paste("one", 2, "three", 4, 5, sep = '')
[1] "one2three45"

Keep in mind that the sep argument doesn’t work if you use a vector as the input argument:

paste(1:3, sep = '_')
[1] "1" "2" "3"

Instead, you should use the collapse argument, which provides the character used to separate the elements of a vector:

paste(1:3, collapse = '_')
[1] "1_2_3"

One of the most powerful capabilities of the paste() function is mixing elements of a vector with standalone elements:

paste(c("a", "b"), "c", "d")
[1] "a c d" "b c d"

In this case, the output is a vector of two character elements, each of which is the result when you concatenate an element of the vector with the other input arguments in the paste() command.

You can even mix two vectors with this function:

paste(c("a", "b"), c("c","d"))
[1] "a c" "b d"

These are the results when these vectors don’t have the same length:

paste(c("a", "b"), c("c","d", "e"))
[1] "a c" "b d" "a e"
paste(c("a", "b", "c"), c("d","e"))
[1] "a d" "b e" "c d"

Remember that the sep and collapse arguments still work when you mix different R objects like this. In particular, the sep string separates the terms from different input arguments, and the collapse string separates those results in the final string.

Here is how the paste() function creates a single character output from two vectors:

paste(c("a", "b"), c("c","d"), sep = '-', collapse = ', ')
[1] "a-c, b-d"


You can use the paste() function in R to concatenate elements from multiple R objects into strings. The returned value can be a single element or a vector of character elements, depending on the input objects and the delimiter strings you provide. For more complex data structures like tables, read this guide.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *