[R] backslash quirk in paste

Prof J C Nash (U30A) nashjc at uottawa.ca
Sat Dec 6 20:00:46 CET 2014


This is NOT critical. It arose due to a fumble fingers when developing
an R example, but slightly intriguing.

How could one build a string from substrings with a single backslash (\)
as separator. Here's the reproducible example:

fname = "John"; lname = "Smith"
paste(fname, lname)
paste(fname, lname, sep=" / ")
# BUT there's a glitch with backslash
paste(fname, lname, sep=" \ ") # because of escaping character
paste(fname, lname, sep=' \ ')
paste(fname, lname, sep=' \\ ') # because of escaping character
bslash <- "\\"
print(bslash)
paste(fname, lname, sep=bslash)

Possibly the answer is that R never allows a single backslash in its
strings, but I can imagine possible cases where I might want to output
such lines, for example, in documenting this.

Best, JN



More information about the R-help mailing list