Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display text with Quotes in R? [duplicate]

Tags:

string

r

quotes

I have started learning R and am trying to create vector as below:

c(""check"")

I need the output as : "check". But am getting syntax error. How to escape the quotes while creating a vector?

like image 578
knix2 Avatar asked Mar 04 '13 14:03

knix2


3 Answers

As @juba mentioned, one way is directly escaping the quotes.

Another way is to use single quotes around your character expression that has double quotes in it.

> x <- 'say "Hello!"'
> x
[1] "say \"Hello!\""
> cat(x)
say "Hello!"
like image 199
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 19 '22 16:10

A5C1D2H2I1M1N2O1R2T1


Use a backslash :

x <- "say \"Hello!\""

And you don't need to use c if you don't build a vector.

If you want to output quotes unescaped, you may need to use cat instead of print :

R> cat(x)
say "Hello!"
like image 30
juba Avatar answered Oct 19 '22 16:10

juba


Other answers nicely show how to deal with double quotes in your character strings when you create a vector, which was indeed the last thing you asked in your question. But given that you also mentioned display and output, you might want to keep dQuote in mind. It's useful if you want to surround each element of a character vector with double quotes, particularly if you don't have a specific need or desire to store the quotes in the actual character vector itself.

# default is to use "fancy quotes"
text <- c("check")
message(dQuote(text))
## “check”

# switch to straight quotes by setting an option
options(useFancyQuotes = FALSE)
message(dQuote(text))
## "check"

# assign result to create a vector of quoted character strings
text.quoted <- dQuote(text)
message(text.quoted)
## "check"

For what it's worth, the sQuote function does the same thing with single quotes.

like image 39
regetz Avatar answered Oct 19 '22 14:10

regetz