[R] backslash quirk in paste

William Dunlap wdunlap at tibco.com
Sat Dec 6 20:35:12 CET 2014


"\\" is stored as a single backslash, just as "\n" is a single newline
character.  It is printed with an extra backslash.

  > nchar("\\")
  [1] 1
  > cat(paste0("\\", "\n"))
  \



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Dec 6, 2014 at 11:00 AM, Prof J C Nash (U30A) <nashjc at uottawa.ca>
wrote:

> 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
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list