I'm trying to use the regexpr function to locate the decimal in a character string. Regardless of the position of the decimal, the function returns 1. For example,> regexpr(".", "Female.Alabama")[1] 1 attr(,"match.length") [1] 1 In trying to figure out what was going on here, I tried the below command:> gsub(".", ",", "Female.Alabama")[1] ",,,,,,,,,,,,,," It looks like R is treating every character in the string as if it were decimal. I didn't see anything in the help file about "." being some kind of special character. Any idea why R is treating a decimal this way in these functions? Any suggestions how to get around this? Thanks for any suggestions. -Trevor [[alternative HTML version deleted]]
Thompson, Trevor wrote:> It looks like R is treating every character in the string as if it were > decimal. I didn't see anything in the help file about "." being some kind > of special character. Any idea why R is treating a decimal this way in > these functions? Any suggestions how to get around this?'.' is the regexpr character for matching any single character! > regexpr("a.e", "Female.Alabama") [1] 4 To actually search for a dot, you need to 'escape' it with a backslash, but of course the backslash needs escaping itself, with another backslash. Luckily that backslash doesn't need escaping, otherwise we would quickly run out of patience. > regexpr("\\.", "Female.Alabama") [1] 7 Baz
Try regexpr("\\.", "Female.Alabama")
-----Original Message-----
From: Thompson, Trevor [mailto:tkt2 at cdc.gov]
Sent: 13 August 2003 15:47
To: r-help at stat.math.ethz.ch
Subject: [R] Regexpr with "."
I'm trying to use the regexpr function to locate the decimal in a
character string. Regardless of the position of the decimal, the
function returns 1. For example,
> regexpr(".", "Female.Alabama")
[1] 1
attr(,"match.length")
[1] 1
In trying to figure out what was going on here, I tried the below
command:
> gsub(".", ",", "Female.Alabama")
[1] ",,,,,,,,,,,,,,"
It looks like R is treating every character in the string as if it were
decimal. I didn't see anything in the help file about "." being
some
kind of special character. Any idea why R is treating a decimal this
way in
these functions? Any suggestions how to get around this?
Thanks for any suggestions.
-Trevor
[[alternative HTML version deleted]]
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> I'm trying to use the regexpr function to locate the decimal in a character > string. Regardless of the position of the decimal, the function returns 1.You need to escape it.> gsub("\\.",",","Female.Alabama")[1] "Female,Alabama"
Try
regexpr("\\.", "Female.Alabama") and gsub("\\.",
",", "Female.Alabama")
>X-Sybari-Trust: 9293cd92 d90ef28b 235e1558 0000093d
>From: "Thompson, Trevor" <tkt2 at cdc.gov>
>To: r-help at stat.math.ethz.ch
>Date: Wed, 13 Aug 2003 10:46:45 -0400
>MIME-Version: 1.0
>X-Virus-Scanned: by amavisd-milter (http://amavis.org/)
>X-Virus-Scanned: by amavisd-milter (http://amavis.org/)
>X-Spam-Status: No, hits=0.6 required=5.0 tests=HTML_30_40 version=2.54
>X-Spam-Level:
>X-Spam-Checker-Version: SpamAssassin 2.54 (1.174.2.17-2003-05-11-exp)
>Content-Disposition: inline
>Content-Transfer-Encoding: 7bit
>Subject: [R] Regexpr with "."
>X-BeenThere: r-help at stat.math.ethz.ch
>X-Mailman-Version: 2.1.2
>List-Id: Main R Mailing List: Primary help <r-help.stat.math.ethz.ch>
>List-Help: <mailto:r-help-request at stat.math.ethz.ch?subject=help>
>List-Post: <mailto:r-help at stat.math.ethz.ch>
>List-Subscribe:
<https://www.stat.math.ethz.ch/mailman/listinfo/r-help>,
<mailto:r-help-request at
stat.math.ethz.ch?subject=subscribe>>List-Archive: <https://www.stat.math.ethz.ch/pipermail/r-help>
>List-Unsubscribe:
<https://www.stat.math.ethz.ch/mailman/listinfo/r-help>,
<mailto:r-help-request at
stat.math.ethz.ch?subject=unsubscribe>>
>I'm trying to use the regexpr function to locate the decimal in a
character
>string. Regardless of the position of the decimal, the function returns 1.
>For example,
>
>> regexpr(".", "Female.Alabama")
>[1] 1
>attr(,"match.length")
>[1] 1
>
>In trying to figure out what was going on here, I tried the below command:
>
>> gsub(".", ",", "Female.Alabama")
>[1] ",,,,,,,,,,,,,,"
>
>It looks like R is treating every character in the string as if it were
>decimal. I didn't see anything in the help file about "."
being some kind
>of special character. Any idea why R is treating a decimal this way in
>these functions? Any suggestions how to get around this?
>
>Thanks for any suggestions.
>
>-Trevor
>
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Jianhua Zhang
Department of Biostatistics
Dana-Farber Cancer Institute
44 Binney Street
Boston, MA 02115-6084
Thompson, Trevor wrote:> I'm trying to use the regexpr function to locate the decimal in a character > string. Regardless of the position of the decimal, the function returns 1. > For example, > > >>regexpr(".", "Female.Alabama") > > [1] 1 > attr(,"match.length") > [1] 1 > > In trying to figure out what was going on here, I tried the below command: > > >>gsub(".", ",", "Female.Alabama") > > [1] ",,,,,,,,,,,,,," > > It looks like R is treating every character in the string as if it were > decimal. I didn't see anything in the help file about "." being some kind > of special character. Any idea why R is treating a decimal this way in > these functions? Any suggestions how to get around this? > > Thanks for any suggestions. > > -Trevor > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-helpThink about the meaning of "." in regular expression (it needs to be escaped as being a special character!): gsub("\\.", ",", "Female.Alabama") or regexpr("\\.", "Female.Alabama") Uwe Ligges
Thompson, Trevor wrote:> I'm trying to use the regexpr function to locate the decimal in a character > string. Regardless of the position of the decimal, the function returns 1. > For example, > >>regexpr(".", "Female.Alabama")You probably want backslashes to indicate that "." should not be treated as a metacharacter; it should be taken literally. > regexpr("\\.", "Female.Alabama") [1] 7 attr(,"match.length") [1] 1 hope this helps, Chuck Cleland
Trevor, The "." is a regex meta-character that matches any character. In order to look specifically for a ".", the you must escape it with a "\", and that "\" must also be escaped, thus,> regexpr("\\.", "Female.Alabama")[1] 7 attr(,"match.length") [1] 1>HTH steve "Thompson, Trevor" wrote:> I'm trying to use the regexpr function to locate the decimal in a character > string. Regardless of the position of the decimal, the function returns 1. > For example, > > > regexpr(".", "Female.Alabama") > [1] 1 > attr(,"match.length") > [1] 1 > > In trying to figure out what was going on here, I tried the below command: > > > gsub(".", ",", "Female.Alabama") > [1] ",,,,,,,,,,,,,," > > It looks like R is treating every character in the string as if it were > decimal. I didn't see anything in the help file about "." being some kind > of special character. Any idea why R is treating a decimal this way in > these functions? Any suggestions how to get around this? > > Thanks for any suggestions. > > -Trevor > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help