paste in R: How to Use paste() Function in R

To concate strings in R, use the paste() function. The paste() function converts its input arguments to character strings and concatenates them.

paste() in R

The paste() is a built-in R function used to concatenate vectors by converting them into character. The paste() method takes three parameters, and returns concatenated string.

The paste0() function in R concatenates the vector without any separator.

  1. The paste() function concatenates the vectors or strings.
  2. The paste() function concatenates the columns of the data frame.
  3. The paste0() function concatenates the vectors or strings without any separator.
  4. The paste0() function concatenates the columns of the data frame.
  5. The paste() function uses a space to join strings, paste0() function defaults sep to the empty string.

Syntax

The syntax of the paste() function is,

paste(x, sep=" ", collapse=NULL)

Parameters

The x is one or more R objects to be concatenated together.

The sep is a character string to separate the terms.

The default value of collapse is NULL. It is an optional character string to separate the results.

Example

By default, the paste() function in R will accept an unlimited number of scalars and join them together, separating each scalar with space.

paste("Hello", 19, 21, "Mate")

Output

[1] "Hello 19 21 Mate"

Passing separator argument to paste() function

If you pass the separator argument to the paste() function, it will separate the elements.

paste("Hello", 19, 21, "Mate", sep = "_")

Output

[1] "Hello_19_21_Mate"

You can see that underscore(_) separator separates the scalars. The sep= argument controls what is placed between each set of values when they are combined.

Paste() function in R with collapse argument

The collapse argument can define a value to use when joining those values to create a single string.

When you pass a vector to a paste() function, the separator parameter will not work. That is why we use the collapse parameter, which is beneficial when you are dealing with the vectors.

paste(c("K", "D", "L"), 1:9, sep = "|", collapse = " & ")

Output

[1] "K|1 & D|2 & L|3 & K|4 & D|5 & L|6 & K|7 & D|8 & L|9"

In this example, the separator (|) will deal with the values which are to be placed in between the set of items and the collapse argument (&) will make use of specific value to concatenate the items into a single string.

Using paste0() function in R

The paste0() is a built-in R function that acts just like the paste() function but with a default(“”) separator.

paste0("K", "D", "L")

Output

[1] "KDL"

Paste0() with collapse argument

The collapse argument in the paste0() function is the character, symbol, or value used to separate the elements.

paste0("KDL", 1:3, collapse = "_")

Output

[1] "KDL1_KDL2_KDL3"

You can see from the output that the paste0() function returns a string with a default separator and a specified collapse argument.

Conclusion

The paste() function is like concatenation using separation factor, whereas the paste0() is like append function using separation factor. That is it for this tutorial.

Leave a Comment