R Program to Concatenate Two Strings

[This article was first published on R feed, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Concatenate means joining multiple strings into one. In R, we use the paste() function to concatenate two or more strings.


Example 1: Concatenate Strings in R

# create two strings
string1 <- "Programiz"
string2 <- "Pro"
 
# using paste() to concatenate two strings
result = paste(string1, string2)
 
print(result)

Output

[1] "Programiz Pro"

In the above example, we have passed strings: string1 and string2 inside the paste() function to concatenate two strings.

The default separator in the paste() function is whitespace " ". So "Programiz" and "Pro" are joined with whitespace in between them.

We can specify our own separator by passing the sep parameter.


Example 2: Concatenate Strings Using a Separator

# create two strings
string1 = "Programiz"
string2 = "Pro"
 
# concatenate two strings using separator
result = paste(string1, string2, sep = "-")
 
print(result)

Output

[1] "Programiz-Pro"

Here, we have passed the sep parameter inside the paste() function to concatenate two strings: string1 and string2 with a hyphen in between them.

To leave a comment for the author, please follow the link and comment on their blog: R feed.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)