In this tutorial, we will learn how to Concatenate Strings in R programming Language.

To concatenate strings in r programming, use paste() function. The syntax of paste function that is used to concatenate two or more strings.

where

pasteis the keyword
input strings separated by comma
sepis a character that would be appended between two adjacent strings and acts as a separator
collapseis an optional character to separate the results

Concatenate two or more Strings in R

While concatenating strings in R, we can choose the separator and number number of input strings. Following examples demonstrate different scenarios while concatenating strings in R using paste() function.

Example: Concatenate Strings in R

In this example, we will use paste() function with default separator.

The default separator in paste function is space " ". So ‘Hello’ and ‘World!’ are joined with space in between them.

Example: Concatenate Strings in R with no separator

In this example, we shall use paste() function with the separator as emtpy, i.e., sep="".

Please observe that we have overwritten default separator in paste function with “”. So ‘Hello’ and ‘World!’ are joined with nothing in between them.

Example: Concatenate Multiple Strings

In this example, we shall use paste() function with the separator as hyphen, i.e., sep="-" and also we take multiple strings at once.

You may provide as many strings as required followed by the separator to the R paste() function to concatenate.

Concluding this R Tutorial, we have learned to concatenate two or more strings using paste() function with the help of example programs.