Philippe Hupé
2003-Sep-11 07:54 UTC
[R] how to insert a double quote with the paste function?
Hello, 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. Thanks, Philippe -- -------------------------------------------------- Philippe Hup? Institut Curie - Equipe Bioinformatique 26, rue d'Ulm - 75005 PARIS France +33 (0)1 44 32 42 75 Philippe.Hupe at curie.fr <mailto:Philippe.Hupe at curie.fr>
Philippe Glaziou
2003-Sep-11 08:10 UTC
[R] how to insert a double quote with the paste function?
Philippe Hup? <Philippe.Hupe at curie.fr> 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.How about:> h<-'hello "World"' > h[1] "hello \"World\""> cat(h, "\n")hello "World">-- Philippe Glaziou Institut Pasteur du Cambodge
David Khabie-Zeitoune
2003-Sep-11 08:15 UTC
[R] how to insert a double quote with the paste function?
Philippe You seem to have the right answer already, unless I misunderstand... If I define:> x = "Hello \"World\""Then the default print method for this character string will give the rather ugly looking:> x[1] "Hello \"World\"" But this is just R telling us that it does recognise the escape sequence \". If we pass this variable to a more output-friendly function like cat, we get:> cat(x)Hello "World" So the variable x really does contain 'Hello "World"', it's just that R's default print method will show the escape characters used in a string. -----Original Message----- From: Philippe Hup? [mailto:Philippe.Hupe at curie.fr] Sent: 11 September 2003 08:55 To: 'r-help at stat.math.ethz.ch' Subject: [R] how to insert a double quote with the paste function? Hello, 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. Thanks, Philippe -- -------------------------------------------------- Philippe Hup? Institut Curie - Equipe Bioinformatique 26, rue d'Ulm - 75005 PARIS France +33 (0)1 44 32 42 75 Philippe.Hupe at curie.fr <mailto:Philippe.Hupe at curie.fr> ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Prof Brian Ripley
2003-Sep-11 08:18 UTC
[R] how to insert a double quote with the paste function?
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
Patrick Burns
2003-Sep-11 08:24 UTC
[R] how to insert a double quote with the paste function?
The paste is doing the right thing -- it is how it is displayed that matters: > jj <- paste("hello", "\"world\"") > jj [1] "hello \"world\"" > cat(jj) hello "world"> print(jj, quote=FALSE) [1] hello "world" Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Philippe Hup? wrote:> Hello, > > 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. > > Thanks, > > Philippe