[R] how to manually enter an double quote as data feed?
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Wed Dec 14 00:58:10 CET 2011
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of bonnieyuan
> Sent: Tuesday, December 13, 2011 2:04 PM
> To: r-help at r-project.org
> Subject: [R] how to manually enter an double quote as data feed?
>
> I'm doing a text mining project where I have to manually enter a double
> quote
> as an element inside a vector.
>
> I tried
>
> char[10]='"'#where i enclosed the double quote in a pair of single
> quotes.
>
> But the result is [1] "\"". Somehow a back slash is added
> automatically.
>
> I also tried to enclose the double quote in a pair of double quotes.
> That
> didn't work either.
>
> I'm using Mac and latest release of R.
>
> Thank you!
>
> Bonnie Yuan
>
Bonnie,
The backslach character in not actually there. Since the double quote character has special meaning in R, it needs to be 'escaped' to change its special meaning to become a literal double quote character. That is the function of the \ escape character.
Try the following
s1 <- '"' #assign as you did
s2 <- "\"" #assign using escape character
s1 #displays with backslash
nchar(s1) #but there is only 1 character in the string
s2
nchar(s2)
print(s1) #prints backslash indicating literal double quote
cat(s1,'\n') #cat function displays without the backslash
#notice n character is escaped, creating a newline character
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list