How to Use paste0() Function in R

R paste0() function is used to concatenate vectors after converting them to character vectors without a separator.

Syntax

paste0(…, collapse = NULL)

Parameters

  1. …: It is one or more R objects to be converted to character vectors.
  2. collapse: It is an optional character string to separate the results.

Visual Representation

How to Use paste0() functionExample 1: How to Use paste0() function

paste0("Data", "Matics")

paste0(letters[18:22])

Output

[1] "DataMatics"
[1] "r" "s" "t" "u" "v"

Example 2: Passing ‘collapse’ argument

paste0("Data", collapse = " ")
paste0("Dog", collapse = "/")
paste0(letters[18:22], collapse = "*")
paste0("K", 1:7)

Output

[1] "Data"
[1] "Dog"
[1] "r*s*t*u*v"
[1] "K1" "K2" "K3" "K4" "K5" "K6" "K7"

Difference between paste() and paste0() function

The main difference between the paste() and paste0() functions is that the paste() function uses a separator, whereas the paste0() function does not use a separator.

st <- paste("Eleven", "Mike", "Dustin", sep = "")
st

# Default value of sep with paste function
st1 <- paste0("Eleven", "Mike", "Dustin")
st1

Output

[1] "ElevenMikeDustin"
[1] "ElevenMikeDustin"

The paste() and paste0() functions combine several inputs into a character string.

The paste0() is faster than paste() if our objective is to concatenate strings without spaces because we don’t have to specify the argument sep.

Leave a Comment