Skip to content

escaping backslash in a string

8 messages · Peter Dalgaard, Srinivas Iyyer, James W. MacDonald +2 more

#
How can I get a single backslash in a character string?

My goal is to escape dots in a string that will be used as a regular
expression. I thought I could do it this way:

gsub(".", "\\.", x)

Unfortunately, "\\" does not represent a literal backslash as I
expected, but rather a pair of backslashes:
[1] "\\."
[1] "\\"

Just a backslash and a dot fails too, since that represents an escaped dot:
[1] "."

A single backslash works in the middle of strings sometimes,but it
depends on what the character following it is (presumably depending on
whether the pair of characters represents an escape sequence):
[1] "a\b"
[1] "xy"

Is there a way to represent "\"? This seems like a design problem in
the interpreter.
_                    
platform i386-redhat-linux-gnu
arch     i386                 
os       linux-gnu            
system   i386, linux-gnu      
status                        
major    2                    
minor    0.1                  
year     2004                 
month    11                   
day      15                   
language R
#
Dan Lipsitt <danlipsitt at gmail.com> writes:
Nononononono.... If you want to know what is inside a string, use
cat() not (implicitly) print()
\.>

The thing is that print() itself escapes "weird" characters, including
the escape character:
\.
[1] "\\."
Yes. Not at all.
#
Dear group, 
 I am using justRMA in bioconductor to get expression
values.  Now I computed fold changes.  the fold
changes i get are log2 values.  

How can I convert these to log10 values to see them as
actual fold change values. 

thank you.
#
Srinivas Iyyer <srini_iyyer_bio at yahoo.com> writes:
Divide by log2(10) or multiply by log10(2).
#
Srinivas Iyyer wrote:
First, a bit of etiquette. It is considered impolite to post the same 
question to more than one listserv. Second, although this is not a 
BioC-specific question, that is probably a better place to ask.

To answer; log10 values will *not* give you actual fold changes. These 
will be the same as log2, to a multiplicitive constant. You want to 
convert back to the natural scale e.g., 2^FC, where FC = your fold changes.

Also note that you compute fold change on the log scale by subtraction, 
not division.

HTH,

Jim

  
    
#
I have it working now, I think. Since it's going into a regular
expression, I have to escape each of the escape characters, resulting
in four backslashes altogether:
[1] "axb"
[1] "a.b"
[1] "a.b"
[1] "a.b"
[1] "a\\.b"
a\.b>

or
a\.b> 

Dan
#
Dan Lipsitt <danlipsitt <at> gmail.com> writes:

: 
: I have it working now, I think. Since it's going into a regular
: expression, I have to escape each of the escape characters, resulting
: in four backslashes altogether:
: 
: > sub("[.]", "x", "a.b")
: [1] "axb"
: > sub("[.]", "\.", "a.b")
: [1] "a.b"
: > sub("[.]", "\\.", "a.b")
: [1] "a.b"
: > sub("[.]", "\\\.", "a.b")
: [1] "a.b"
: > sub("[.]", "\\\\.", "a.b")
: [1] "a\\.b"
: > cat(sub("[.]", "\\\\.", "a.b"))
: a\.b>
: 
: or
: 
: > cat(sub("\\.", "\\\\.", "a.b"))
: a\.b> 
: 

You can use \134 in place of the double backslash 
if that makes more sense to you.  

Another possibility is to create a variable 
   backslash <- "\\" 
and paste together each string in terms of that variable.

Also its sometimes helpful to use nchar(s) on string s
just to check how many characters it has.