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")
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."
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."
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."
…and paste0:
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
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"
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"
Only one string left… Looks good!
Tutorial Video & Further Resources for the Handling of Characters in R
For more detailed information concerning the code of this article, please check out the below video on the Statistics Globe YouTube channel:
In case you need still 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
- The print and cat R Functions
- Concatenate Character Strings in R
- Convert Character to Numeric
- The get Function in R
- 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!
Leave a Reply
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Related Tutorials