How to remove quotes from a string in R (with examples)

There are three different formulas that will remove the quotation marks for a string. The first and the last produce similar results while the cat function produces two different ways of displaying the list. The one that you use depends upon the situation.

Description

There are actually three functions that perform this task. The first is noquote(string), this function speaks for itself, and it is simple to use because you only need to insert the variable being worked on between the brackets. The second function cat(string) is less intuitive in its name but works the same way. The final function is print(string, quote = FALSE) which is an ordinary print statement but with an additional argument to remove the quotes.

Explanation

These functions accomplish essentially the same task, but the cat function produces slightly different results. These differences include using paste(strong, “\n”) to produce a column rather than a row. The noquote function differs from the print function in that the print function requires an extra argument to remove the quotes. The noquote function is basically a special variant of the print function dedicated to removing quotes from strings.

Examples

Here we have four different ways to remove quotes from a string. They each illustrate a different method.

> x = c(“Hello”, “Goodbye”, “Open”, “Close”)
> x
[1] “Hello” “Goodbye” “Open” “Close”

Here is a simple example of the original formula showing up as originally intended.

> x = c(“Hello”, “Goodbye”, “Open”, “Close”)
> noquote(x)
[1] Hello Goodbye Open Close

Here is an example of using the noquote function to remove quotation marks while maintaining the strings.

> x = c(“Hello”, “Goodbye”, “Open”, “Close”)
> cat(x)
Hello Goodbye Open Close

The quotations can also be removed using the cat function, which in this format simply removes the quotes.

> x = c(“Hello”, “Goodbye”, “Open”, “Close”)
> cat(paste(x, “\n”))
Hello
Goodbye
Open
Close

This example also uses the cat function, but it puts each element in a column rather than a row.

> x = c(“Hello”, “Goodbye”, “Open”, “Close”)
> print(x, quote = FALSE)
[1] Hello Goodbye Open Close

This final example uses the print function with quotation marks set to false so as to remove them.

Application

The main application of these functions is being able to display one or more strings without including quotation marks. The cat function has the added application of being able to produce a column rather than a row. Being able to produce a column comes in handy when you are printing out a list, particularly a list consisting of complete sentences rather than individual words. Having three different functions provide an added degree of flexibility.

Removing the quotation marks from a string allows you to make more naturally looking printouts of the data. Because this is a handy way of displaying string data these functions are handy tools for your R programming toolbox.

Scroll to top
Privacy Policy