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 -- 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
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, > LiviuPresuming 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
On 15-May-09 14:46:27, 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\\%" "70\\%" "100\\%" > > Could anyone suggest how to obtain output similar to: > [1] "mean" "sd" "0\%" "25\%" "50\%" "75\%" "100\%" > > Thank you, > Liviu1: The double escape "\\" is the correct way to do it. If you give "\%" to gsub, it will try to interpret "%" as a special character (like "\n" for newline), and there is none such (as it tells you). On the other hand, "\\" tells gsub to interpret "\" (normally used as the Escape character) in a special way (namely as a literal "\"). 2: The output "mean" "sd" "0\\%" "25\\%" "50\\%" "70\\%" "100\\%" from gsub("%", "\\%", temp1, fixed=TRUE) is one of those cases where R displays something different from what is really there! In other words, "0\\%" for example is the character string you would have to enter in order for R to store \%. You can see what is really there using cat: cat(gsub("%", "\\%", temp1, fixed=TRUE)) # mean sd 0\% 25\% 50\% 75\% 100\% which, of course, is what you wanted. You can see in other ways that what is stored is what you wanted -- for instance: temp2 <- gsub("%", "\\%", temp1, fixed=TRUE) write.csv(temp2,"gsub.csv") and then, if you look into gsub.csv outside of R, you will see: "","x" "1","mean" "2","sd" "3","0\%" "4","25\%" "5","50\%" "6","75\%" "7","100\%" which, again, is what you wanted. Hoping this helops, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 15-May-09 Time: 16:32:13 ------------------------------ XFMail ------------------------------
Thanks all for the prompt responses. Now Hmisc::latex() no longer generates errors on Rcmdr::numSummary() objects (with `tempa' below being such an object).> colnames(tempa$table) <- gsub("%", "\\%", colnames(tempa$table), fixed=TRUE) > latex(tempa$table, cdec=3)Best regards, Liviu On Fri, May 15, 2009 at 5:13 PM, Patrick Burns <pburns at pburns.seanet.com> wrote:> See 'The R Inferno' page 46. > > > > Patrick Burns > patrick at burns-stat.com > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of "The R Inferno" and "A Guide for the Unwilling S User") > > 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 >> >> >> >-- 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
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-mailMichael Dewey http://www.aghmed.fsnet.co.uk