Create Character String with Quotes in R (2 Examples)
In this tutorial you’ll learn how to display a text with quotes in R programming.
The tutorial will consist of two examples for the displaying of a text with quotes. More precisely, the tutorial contains these contents:
Let’s dive into it…
Example 1: Display Text with Double Quotation Marks
This example illustrates how to show double quotes in a character string.
For this task, we have to create the character string using single quotes:
x_double <- 'This is "my ""text with double ""quotes"' # Create character string x_double # Print character string # [1] "This is \"my \"\"text with double \"\"quotes\""
As you can see based on the previous output of the RStudio console, we have created a new character string object containing multiple double quotes.
In the output above, you can also see that the quotes are displayed with a slash in front. We can change that using the cat function:
cat(x_double) # Apply cat function to string # This is "my ""text with double ""quotes"
We have removed the single quotes and the slashes from the output above.
Example 2: Display Text with Single Quotation Marks
Similar to Example 1, we can also display single quotes in a character string.
This can be achieved by using double quotes to construct our character string:
x_single <- "This is 'my ''text with single ''quotes'" # Create character string x_single # Print character string # [1] "This is 'my ''text with single ''quotes'"
As in Example 1, we can use the cat function to avoid the single quotes in the RStudio console output:
cat(x_single) # Apply cat function to string # This is 'my ''text with single ''quotes'
Video, Further Resources & Summary
In case you need more explanations on the R programming syntax of this tutorial, you might want to have a look at the following video that I have published on my YouTube channel. I explain the R programming code of this article in the video:
Furthermore, you may read the other tutorials on https://www.statisticsglobe.com/:
- Error: ‘R’ is an unrecognized escape in character string starting “”C:R”
- Escape Backslash in Character String
- Remove All White Space from Character String
- parse, deparse & expression Functions in R
- Don’t Display Quotes when Printing Character String to R Console
- All R Programming Tutorials
Summary: In this article you have learned how to show quotes in a character string in the R programming language. Please let me know in the comments section, if you have any additional questions on how to print text data properly.