# paste & paste0 R Functions (4 Examples)

This article explains how to use the **paste and paste0 functions** in R. First, let’s have a look at the basic R syntax and the definition of the two functions:

**Basic R Syntax:**

paste("char1", "char2", sep = " ") paste0("char1", "char2") |

paste("char1", "char2", sep = " ") paste0("char1", "char2")

**Definition:**

The paste & paste0 functions **combine several inputs into a character string**.

In this tutorial, I’ll show you **four examples** for the application of paste and paste0 in the R programming language.

So without further ado, let’s dive in…

## Example 1: Basic Application of the paste Function in R

The following R code illustrates the basic functionality of the paste R function:

paste("This is", 1, "out of", 4, "examples.") # Basic application of paste # "This is 1 out of 4 examples." |

paste("This is", 1, "out of", 4, "examples.") # Basic application of paste # "This is 1 out of 4 examples."

As you can see based on the previous R code, we inserted several inputs into the paste function:

- The character string “This is”.
- The numeric value 1.
- The character string “out of”.
- The numeric value 4.
- The character string “examples.”.

The paste function concatenated all these inputs into the character string “This is 1 out of 4 examples.”

Quite easy! However, there are several things to consider when applying paste in R. More on that in the following examples…

## Example 2: Change Separator of paste Function

An important specification within the paste function is the separator (i.e. the character string that is inserted between the inputs).

By default, the paste function uses a blank as separator (i.e. sep = ” “). However, we can manually adjust the separator as follows:

paste("This is", 1, "out of", 4, "examples.", # paste with different separator sep = "___") # "This is___1___out of___4___examples." |

paste("This is", 1, "out of", 4, "examples.", # paste with different separator sep = "___") # "This is___1___out of___4___examples."

In this example, we used the separator “___”. However, we could use basically every character string we want. And this is exactly the difference between paste and paste0…

## Example 3: The Difference Between paste & paste0

The paste0 function is a simplified form of the paste function, which uses no separator at all. Have a look at the R help documentation of paste and paste0:

**Figure 1: Excerpt of the R Help Documentation of paste() & paste0().**

As you can see based on Figure 1, the paste0 command doesn’t provide a separator option (as paste does). What’s the reason?

By default, paste0 uses sep = “” (i.e. no separator at all). This can be convenient, in case you don’t want to use a separator anyway. However, at the same time you are limited in your output possibilities.

Consider the following two examples of paste…

paste("This is", 1, "out of", 4, "examples.", sep = "") # Difference between paste & paste0 # "This is1out of4examples." |

paste("This is", 1, "out of", 4, "examples.", sep = "") # Difference between paste & paste0 # "This is1out of4examples."

…and paste0:

paste0("This is", 1, "out of", 4, "examples.") # paste0 - separator not needed # "This is1out of4examples." |

paste0("This is", 1, "out of", 4, "examples.") # paste0 - separator not needed # "This is1out of4examples."

Both R codes print the same output to the RStudio console.

## Example 4: Using the collapse Option of paste & paste0

The collapse option is mainly used to concatenate vectors of strings together. Consider the following example vector:

x <- c("another", "example", "with", "a", "vector") # Create example vector of strings |

x <- c("another", "example", "with", "a", "vector") # Create example vector of strings

If we would just insert this example vector into the paste (or paste0) function, the output would be as follows:

paste(x) # paste vector without collapse # "another" "example" "with" "a" "vector" |

paste(x) # paste vector without collapse # "another" "example" "with" "a" "vector"

We just created a vector instead of a single character string (as you can see based on the quotes of the RStudio output). If we want to combine these vector elements into a single character string, we can use the collapse option:

paste(x, collapse = " ") # paste vector with collapse = " " # "another example with a vector" |

paste(x, collapse = " ") # paste vector with collapse = " " # "another example with a vector"

Only one string left… Looks good!

## Further Resources for the Handling of Characters in R

In case you need more examples for the pasting in R, you could have a look at the following YouTube video of Richard Webster. In the video, he is giving more examples for the paste function:

Furthermore, you could also have a look at some of the other tutorials on this website. I have published a decent number of articles on the handling of strings in R already:

- str_c R Function
- sprintf R Function
- Concatenate Character Strings in R
- Convert Character to Numeric
- The get Function in R
- parse, deparse & expression Functions
- The R Programming Language

At this point, you should know how to handle the paste and paste0 functions in R. However, if you have any further comments or questions, you could let me know in the comments. Also, don’t forget to subscribe to my newsletter for regular statistics and programming updates!

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R