R – Concatenate Strings

In this tutorial, we will learn how to concatenate strings in R programming language using paste(), paste0(), separators, and the collapse argument.

String concatenation means joining two or more values to create a single character result. In R, the most commonly used base functions for this are paste() and paste0().

R paste() Function for String Concatenation

To concatenate strings in R programming, use paste() function. By default, paste() places one space between adjacent values. You can change this behavior by passing a value to the sep argument.

The following syntax shows paste() used with an explicitly supplied separator.

</>
Copy
paste(…, sep="", collapse=NULL)

where

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

If sep is not provided, paste() uses a single space as the separator. If you need no separator, pass sep="" or use paste0().

While concatenating strings in R, we can choose the separator, concatenate any number of input values, and combine character vectors into one string when required. The following examples demonstrate different scenarios while concatenating strings in R using paste() function.

Example 1 – Concatenate Two Strings in R with Default Space Separator

In this example, we will use paste() function with default separator, and concatenate two strings.

r_strings_concatenate_two.R

</>
Copy
# Example R program to concatenate two strings

str1 = 'Hello'
str2 = 'World!'

# concatenate two strings using paste function
result = paste(str1,str2)

print (result)

Output

$ Rscript r_strings_concatenate_two.R 
[1] "Hello World!"

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

Example 2 – Concatenate Strings in R with No Separator

In this example, we shall use paste() function with no separator by setting sep="". This joins the values directly without adding a space, comma, hyphen, or any other character between them.

r_strings_concatenate_two.R

</>
Copy
# Example R program to concatenate two strings

str1 = 'Hello'
str2 = 'World!'

# concatenate two strings using paste function
result = paste(str1,str2,sep="")

print (result)

Output

$ Rscript r_strings_concatenate_two.R 
[1] "HelloWorld!"

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

Example 3 – Concatenate Multiple Strings in R with a Hyphen Separator

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

r_strings_concatenate_multiple.R

</>
Copy
# Example R program to concatenate two strings

str1 = 'Hello'
str2 = 'World!'
str3 = 'Join'
str4 = 'Me!'

# concatenate two strings using paste function
result = paste(str1,str2,str3,str4,sep="-")

print (result)

Output

$ Rscript r_strings_concatenate_multiple.R 
[1] "Hello-World!-Join-Me!"

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

Use paste0() to Concatenate R Strings Without Spaces

The paste0() function is a shortcut for paste(..., sep=""). Use it when you want to join strings directly without any separator.

</>
Copy
first <- "Tutorial"
second <- "Kart"

result <- paste0(first, second)
print(result)
[1] "TutorialKart"

For readable sentences, paste() is usually better because it inserts spaces by default. For file names, IDs, codes, or compact labels, paste0() is often simpler.

Concatenate R Strings with Custom Separators

The sep argument controls what is placed between adjacent values. The separator can be a space, comma, hyphen, slash, underscore, or any other string.

</>
Copy
year <- "2026"
month <- "06"
day <- "24"

paste(year, month, day, sep = "-")
paste("chapter", 5, sep = "_")
paste("R", "strings", sep = " / ")
[1] "2026-06-24"
[1] "chapter_5"
[1] "R / strings"
RequirementFunction callResult
Join with one spacepaste("Hello", "World")"Hello World"
Join with no spacepaste("Hello", "World", sep="")"HelloWorld"
Join with no space using shortcutpaste0("Hello", "World")"HelloWorld"
Join with hyphenpaste("Hello", "World", sep="-")"Hello-World"
Join with comma and spacepaste("Hello", "World", sep=", ")"Hello, World"

Concatenate Character Vectors in R with collapse

The collapse argument is used when you want to combine the elements of a character vector into one string. Without collapse, paste() returns a character vector. With collapse, it returns one combined string.

</>
Copy
words <- c("R", "string", "concatenation")

paste(words)
paste(words, collapse = " ")
paste(words, collapse = "-")
[1] "R"             "string"        "concatenation"
[1] "R string concatenation"
[1] "R-string-concatenation"

Use sep to separate values passed side by side. Use collapse to combine multiple results into a single string.

Vectorized String Concatenation in R

paste() and paste0() work with vectors. When vectors are supplied, R joins corresponding elements position by position.

</>
Copy
names <- c("Anil", "Bina", "Charu")
marks <- c(82, 91, 76)

paste(names, marks, sep = ": ")
[1] "Anil: 82"  "Bina: 91"  "Charu: 76"

If one value is shorter, R recycles it to match the longer vector. This is useful for adding the same prefix or suffix to many strings.

</>
Copy
ids <- c(101, 102, 103)

paste0("student_", ids)
[1] "student_101" "student_102" "student_103"

Concatenate Numbers and Strings in R

You can concatenate numbers and strings directly with paste() or paste0(). R converts the non-character values to character form before joining them.

</>
Copy
subject <- "Math"
score <- 95

paste(subject, "score is", score)
paste0(subject, "_", score)
[1] "Math score is 95"
[1] "Math_95"

For formatted numerical output, such as controlling decimal places, use sprintf() before or instead of paste().

</>
Copy
percentage <- 87.456

sprintf("Percentage: %.2f%%", percentage)
[1] "Percentage: 87.46%"

paste(), paste0(), sep, and collapse in R

The following table gives a quick comparison of the main choices used while concatenating strings in R.

R optionUse it whenExample
paste()You want to join values with a space by default.paste("R", "Tutorial")
paste(..., sep="")You want no separator but still want to use paste().paste("R", "Tutorial", sep="")
paste0()You want the shortest way to join values with no separator.paste0("R", "Tutorial")
sepYou want to control what appears between adjacent arguments.paste("A", "B", sep="-")
collapseYou want to combine vector results into one string.paste(c("A", "B"), collapse=",")

Handling NA Values While Concatenating R Strings

When NA is supplied to paste(), it is converted to the text "NA" in the result. If that is not desired, replace missing values before concatenation.

</>
Copy
city <- c("Delhi", NA, "Mumbai")

paste("City:", city)

city[is.na(city)] <- "Unknown"
paste("City:", city)
[1] "City: Delhi"  "City: NA"     "City: Mumbai"
[1] "City: Delhi"   "City: Unknown" "City: Mumbai" 

This is important when concatenating strings for labels, reports, file names, or exported data.

R String Concatenation FAQ

How do you concatenate two strings in R?

Use paste(str1, str2) to concatenate two strings with a space between them. Use paste(str1, str2, sep="") or paste0(str1, str2) to join them without a space.

Which function is used to concatenate elements in R?

The base R functions paste() and paste0() are used to concatenate elements. paste() uses a space by default, while paste0() joins values with no separator.

What does concatenation mean in R?

Concatenation in R means joining values together to create character output. For example, paste("Hello", "World") returns "Hello World".

What is the difference between sep and collapse in paste()?

sep is placed between values passed as separate arguments. collapse combines multiple vector results into one string. Use sep for joining side-by-side values and collapse for reducing a vector to one string.

Can paste() concatenate numbers and strings in R?

Yes. paste() converts numbers and other simple values to character form before joining them. For example, paste("Score", 95) returns "Score 95".

R Concatenate Strings Tutorial QA Checklist

  • Verify that examples distinguish the default paste() separator from sep="".
  • Check that paste0() is explained as the no-separator shortcut.
  • Confirm that sep and collapse are not described as the same operation.
  • Test vectorized concatenation examples for correct output order and recycling behavior.
  • Review NA examples so missing values are handled intentionally before generating labels or file names.

Conclusion: Concatenating Strings in R with paste() and paste0()

In this R Tutorial, we have learned to concatenate two or more strings using paste() function with the help of example programs. We also covered paste0(), custom separators, collapse, vectorized string concatenation, numeric values, and missing values in R string output.