Hi, I have a small R question: how to print a single "\" character? I have the following results:> print("\") does not work> print("\\")[1] "\\" I need to make the following substitution as well, but it does not work either:> sub("_","\_","g_g")[1] "g_g" Thanks in advance, Firas.
On Tue, 19 Apr 2005, Firas Swidan wrote:> Hi, > I have a small R question: how to print a single "\" character? I have the > following results: >> print("\") does not work > >> print("\\") > [1] "\\"Use cat("\\").> I need to make the following substitution as well, but it does not work > either: >> sub("_","\_","g_g") > [1] "g_g"Use sub("_","\\\\_","g_g") If you print() the result it will look as though it has a double \, but that's an optical illusion. cat() will show it correctly with a single \ (and nchar() will confirm that it has 4 characters). -thomas
Firas Swidan wrote:> Hi, > I have a small R question: how to print a single "\" character? I have the > following results: > >>print("\") does not work > > >>print("\\") > > [1] "\\"You can use the lower level function cat() to print exactly what you want: > cat("\\") \> Notice that not even a newline was printed after the backslash, so the prompt showed up on the same line. To get what you were hoping for you could use > cat("[1] \"\\\"\n") [1] "\" but it's pretty hard to read, because of all the escapes.> > I need to make the following substitution as well, but it does not work > either: > >>sub("_","\_","g_g") > > [1] "g_g"I'm not sure what you want to get, but it might be > cat( sub("_", "\\\\_", "g_g") ); cat("\n") g\_g The extra escapes are necessary because you need to send \\ to sub so that it outputs a \. Duncan Murdoch