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

Marc Schwartz marc_schwartz at me.com
Fri May 15 17:10:37 CEST 2009


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.

HTH,

Marc Schwartz




More information about the R-help mailing list