Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a character string containing "

Tags:

string

r

I wish to define a character variable as: a"", as in: my.string <- 'a""' Nothing I have tried works. I always get: "a\"\"", or some variation thereof.

I have been reading the documentation for: grep, strsplit, regex, substr, gregexpr and other functions for clues on how to tell R that " is a character I want to keep unchanged, and I have tried maybe a hundred variations of a"" by adding \\, \, /, //, [], _, $, [, #.

The only potential example I can find on the internet of a string including " is: ‘{}>=40*" years"’ from here: http://cran.r-project.org/doc/manuals/R-lang.html However, that example is for performing a mathematical operation.

Sorry for such a very basic question. Thank you for any advice.

like image 204
Mark Miller Avatar asked Dec 05 '25 15:12

Mark Miller


2 Answers

The backslashes is an artifact of the print method. In fact the default print surrounds your string with quotes. You can disable this by setting argument quote to FALSE.

For example You can use :

print(my.string,quote=FALSE)
[1] a""

But I would use cat or write like this :

 cat(my.string)
a""
 write(my.string,"")
a""
like image 162
agstudy Avatar answered Dec 07 '25 04:12

agstudy


Using substr, one sees that the backslashes seem just to be an artefact of printing:

substr(my.string,2,2)

gives

[1] "\""

also, the string length is as you want it:

> nchar(my.string)
[1] 3

if you want to print your string without the backslashes, use noquote :

> noquote(my.string)
[1] a""
like image 24
Andre Holzner Avatar answered Dec 07 '25 04:12

Andre Holzner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!