top of page

R Program to Concatenate Two Strings

Example 1: Concatenate Strings in R

# create two strings
string1 <- "codes"
string2 <- "withpankaj"
# using paste() to concatenate two strings
result = paste(string1, string2)
[1] "codeswithpankaj"

In the above example, we have passed strings: string1 and string2 inside the


function to concatenate two strings.

The default separator in the paste() function is whitespace " ". So "codes"and "withpankaj"

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 = "codeswithpankaj"
string2 = ""
# concatenate two strings using separator
result = paste(string1, string2, sep = "-")
[1] ""

Here, we have passed the sep parameter inside the paste()

function to concatenate two strings: string1 and string2 with a hyphen in between them.

Related Posts

See All

A Comprehensive Guide to R Data Types

Introduction In the realm of data analysis and statistical computing, the R programming language stands as a powerful tool. Understanding data types in R is fundamental to unleashing its full potentia

R - CSV Files

Getting and Setting the Working Directory You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using setwd()function. # Get


bottom of page