paste0() Function in R

Sheeraz Gul Feb 02, 2024
paste0() Function in R

The paste0() is a built-in function in base R to concatenate all elements without a separator. This tutorial demonstrates how to use the paste0() method in R.

the Paste0() Function in R

The paste0() function concatenate elements without a separator; it also takes one optional argument, collapse. We use paste0() instead of paste() because it is faster and doesn’t need a sep argument.

Let’s try an example.

paste0("Delftstack", ".com")
paste0("Delftstack", " Tutorials")
paste0(letters[5:10])

The code above concatenate the given objects without the sep argument. See output:

[1] "Delftstack.com"

[1] "Delftstack Tutorials"

[1] "e" "f" "g" "h" "i" "j"

Let’s try paste0() with collapse argument, the collapse works similar to the sep argument in some cases. See example:

paste0("Delftstack", "com" , collapse = "*")
paste0("Delftstack", " Tutorials" , collapse = " ")
paste0(letters[5:10] , collapse = "-")

Here the collapse will only work for the third paste0 function. See output:

[1] "Delftstackcom"

[1] "Delftstack Tutorials"

[1] "e-f-g-h-i-j"
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - R Function