[R] detect escape character

Sarah Goslee sarah.goslee at gmail.com
Tue Jun 3 20:37:34 CEST 2014


On Tue, Jun 3, 2014 at 2:29 PM, Frede Aakmann Tøgersen <frtog at vestas.com> wrote:
> Hhhhhmmmhhhhmm.
>
>> mystring <- "Man\\Woman"
>> cat(mystring)
> Man\Woman>
>>
>> mystring <- "Man\Woman"
> Fejl: '\W' is an unrecognized escape in character string starting ""Man\W"

Right. As the comment in my code indicated, \ is a metacharacter, so
you need to escape it with a metacharacter, which happens to be \.

\\ in a string in R represents a single \ in plain text.

And if you read in a file that contains \, it is displayed as \\ in R,
even though it's really a single \.

> Frede Aakmann Tøgersen
> Specialist, M.Sc., Ph.D.
> Plant Performance & Modeling

I find this very entertaining, because that's exactly what I do --
Plant Performance and Modeling -- but when I do it it involves botany.
:)

Sarah

>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf Of Sarah Goslee
>> Sent: 3. juni 2014 20:18
>> To: Adrian Dușa
>> Cc: r-help at r-project.org
>> Subject: Re: [R] detect escape character
>>
>> Hi,
>>
>> R uses \ as a metacharacter, so you need a non-intuitive number of \:
>>
>>
>> mystring <- "Man\\Woman"
>> # \\ is a metacharacter and a \, so it's equivalent to \ in normal text
>> sub("\\\\", "/", mystring)
>>
>>
>> Sarah
>>
>>
>> On Tue, Jun 3, 2014 at 2:03 PM, Adrian Dușa <dusa.adrian at unibuc.ro> wrote:
>> > Dear All,
>> >
>> > I should be knowing this, but not get it right... For a string like this:
>> >
>> > "Man\Woman"
>> >
>> > I would like to detect the escape character "\" and replace it with "/".
>> >
>> > Tried various ways using gsub(), but don't get it right yet. Any suggestion
>> > would be highly welcomed...
>> >
>> > Thank you,
>> > Adrian
>> >
>>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list