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.
- The paste() function concatenates the vectors or strings.
- The paste() function concatenates the columns of the data frame.
- The paste0() function concatenates the vectors or strings without any separator.
- The paste0() function concatenates the columns of the data frame.
- The paste() function uses a space to join strings, paste0() function defaults sep to the empty string.
The syntax of the paste() function is,
paste(x, sep=" ", collapse=NULL)
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.
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")
 "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 = "_")
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 = " & ")
 "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")
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 = "_")
You can see from the output that the paste0() function returns a string with a default separator and a specified collapse argument.
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.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.