[R] how to insert a double quote with the paste function?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 11 10:18:31 CEST 2003


On Thu, 11 Sep 2003, Philippe Hupé wrote:

> I would like to write in a variable the following string :
> 
> Hello "World"
> 
> If I do cat("Hello ","\"World\""), I have the good result on the screen 
> but it can not be affected to a variable. If I do the same thing with 
> paste paste("Hello ","\"World\"") it does not work since \" seems to be 
> not recognized. So what is the trick to do that.

It is recognized:

> xx <- paste("Hello ","\"World\"") 
> print(xx)
[1] "Hello  \"World\""
> cat(xx, "\n")
Hello  "World" 
> print(xx, quote=FALSE)
[1] Hello  "World"

It's likely that it is the behaviour of print() you did not understand: 
the quote appears by itself in the character string but is escaped when 
printed within quotes.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list