[R] replace "%" with "\%"
Michael Dewey
info at aghmed.fsnet.co.uk
Sun May 17 11:08:34 CEST 2009
At 15:46 15/05/2009, Liviu Andronic wrote:
>Dear all,
>I'm trying to gsub() "%" with "\%" with no obvious success.
Hello Liviu,
When I posted a similar question a few years ago one helpful response
introduced me to nchar
> nchar("%")
[1] 1
> nchar("\%")
[1] 1
Warning messages:
1: '\%' is an unrecognized escape in a character string
2: unrecognized escape removed from "\%"
> nchar("\\%")
[1] 2
>
Of course other people have already solved your specific problem but
nchar is a handy tool I find.
(And apologies to the person who helped me last time but I cannot
find the post in the archive for some reason and so am reluctant to
credit them in case I got it wrong)
> > 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
>
>
>
>--
>Do you know how to read?
>http://www.alienetworks.com/srtest.cfm
>Do you know how to write?
>http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
Michael Dewey
http://www.aghmed.fsnet.co.uk
More information about the R-help
mailing list