Don’t Display Quotes when Printing Character String to R Console (3 Examples)

 

In this R tutorial you’ll learn how to remove quotes from a character string when shown in the RStudio console.

The content of the tutorial looks as follows:

If you want to learn more about these contents, keep reading:

 

Creating Example Data

First, we’ll need to create some example data:

my_string <- c("Hello", "XXX", "YaYaYa", "Foo")    # Create vector of strings
my_string                                          # Print strings to RStudio console
# [1] "Hello"  "XXX"    "YaYaYa" "Foo"

As you can see based on the previous output of the RStudio console, the exemplifying data is a vector of character strings with four vector elements.

You can also see the each element of our character vector is quoted. In the following examples, I’ll explain how to get rid of these quotes.

 

Example 1: Printing Character String without Quotes Using noquote() Function

Example 1 illustrates how to print character strings without quotation using the noquote function in R.

noquote(my_string)                                 # Applying noquote
# [1] Hello  XXX    YaYaYa Foo

The previous output of the RStudio console display the character strings of our example vector without quotes. Looks great!

 

Example 2: Printing Character String without Quotes Using cat() Function

Example 2 shows how to show character strings in the RStudio console using the cat function.

Have a look at the following output of the RStudio console:

cat(my_string)                                     # Applying cat
# Hello XXX YaYaYa Foo

We have printed our character strings without any quotes. Note that the cat function also removes the [1] in front of the output.

The cat function can also be used to print each character string without quotes in a new line of the RStudio console. For this, we have to paste “\n” at the end of each character string:

cat(paste(my_string, "\n"))                        # Applying cat & "\n"
# Hello
# XXX
# YaYaYa
# Foo

 

Example 3: Printing Character String without Quotes Using quote Argument of print() Function

Another alternative for printing character strings without quotes is provided by the print function. Within the print function we have to specify the quote argument to be equal to the logical value FALSE:

print(my_string, quote = FALSE)                    # Applying print & quote
# [1] Hello  XXX    YaYaYa Foo

The output is exactly the same as in Example 1, where we have used the noquote function.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. I explain the content of this tutorial in the video.

 

 

In addition, you could have a look at the related tutorials of this website. You can find some interesting tutorials below:

 

In summary: You learned in this post how to get rid of quotation when showing character strings in the R programming language.

In this tutorial, I have shown example syntax for a vector object. However, note that we could apply the same syntax to a data frame column as well.

In case you have any further questions, tell me about it in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top