[R] replace "%" with "\%"

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 17 12:55:56 CEST 2009


Having some way to specify strings which does not involve special
interpretation of backslashes is a frequent wish list item that
would be helpful for latex, Windows path names and regular
expressions.   A review of delimiter collision approaches by
different languages can be found here:

http://en.wikipedia.org/wiki/String_literal#Delimiter_collision

I get the feeling that many people would like to see something here
but that the core group has had a hard time coming to a decision
due to the many possibilities.

On Fri, May 15, 2009 at 12:11 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
> Marc Schwartz wrote:
>>
>> On May 15, 2009, at 9:46 AM, Liviu Andronic wrote:
>>
>>> Dear all,
>>> I'm trying to gsub() "%" with "\%" with no obvious success.
>>>> temp1 <- c("mean", "sd",   "0%",   "25%",  "50%",  "75%",  "100%")
>>>> temp1
>>> [1] "mean" "sd"   "0%"   "25%"  "50%"  "75%"  "100%"
>>>> gsub("%", "\%", temp1, fixed=TRUE)
>>> [1] "mean" "sd"   "0%"   "25%"  "50%"  "75%"  "100%"
>>> Warning messages:
>>> 1: '\%' is an unrecognized escape in a character string
>>> 2: unrecognized escape removed from "\%"
>>>
>>> I am not quite sure on how to deal with this error message. I tried
>>> the following
>>>> gsub("%", "\\%", temp1, fixed=TRUE)
>>> [1] "mean"   "sd"     "0\\%"   "25\\%"  "50\\%"  "75\\%"  "100\\%"
>>>
>>> Could anyone suggest how to obtain output similar to:
>>> [1] "mean"   "sd"     "0\%"   "25\%"  "50\%"  "75\%"  "100\%"
>>>
>>> Thank you,
>>> Liviu
>>
>> Presuming that you might want to output the results to a TeX file for
>> subsequent processing, where the '%' would otherwise be a comment
>> character, the key is not to get a single '\', but a double '\\', so
>> that you then get a single '\' on output:
>>
>> temp1 <- c("mean", "sd",   "0%",   "25%",  "50%",  "75%",  "100%")
>>
>> temp2 <- gsub("%", "\\\\%", temp1)
>>
>> > temp2
>> [1] "mean"   "sd"     "0\\%"   "25\\%"  "50\\%"  "75\\%"  "100\\%"
>>
>> > cat(temp2)
>> mean sd 0\% 25\% 50\% 75\% 100\%
>>
>>
>> Remember that the single '\' is an escape character, which needs to be
>> doubled.
>>
>
> this confusing "backslash each backslashing backslash" scheme is
> idiosyncratic to r;  in many cases where one'd otherwise use a single
> backslash in a regex or a replacement string in another programming
> language, in r you have to double it.
>
> and actually, in this case you don't need four backslashes.  the
> original poster has actually had a valid solution, but he wasn't aware
> that the string "\\%", returned (not printed) by gsub includes two, not
> three characters --  thus only one backslash, not two:
>
>    cat(
>        gsub(
>            pattern='%',
>            replacement='\\%',
>            x='foo % bar',
>            fixed=TRUE))
>    # foo \% bar
>
> of course, if the pattern cannot be fixed, i.e., fixed=TRUE is less than
> helpful, you'd need four backslashes in the replacement -- a cute,
> though somewhat disturbing, weirdo.
>
> vQ
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>




More information about the R-help mailing list