[R] Detecting numerical value in character variable

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Dec 6 09:22:16 CET 2001


On Wed, 5 Dec 2001, Jameson C . Burt wrote:

> I have a variable that can have either numeric or character values.
> When numeric, I take one action; when not-numeric, I take another action.
> Unfortunately, my approaches are awkward, so I look for others' approaches.
>
> To detect a numeric value, I have semi-successfully used two appoaches.
> I somewhat simplify here using direct character values like "123" rather than a variable.
> 1. !is.na(as.numeric("123"))
>    which responds "TRUE", but
>       !is.na(as.numeric("abc"))
>    responds
>       FALSE    #so I know it is not numeric
>       Warning message:
>       NAs introduced by coercion
>    This all works well enough except the error message looks bad
>    when printed, and hints that I use the wrong appoach.

That is the best current approach.  Set options(warn=-1) around the piece
of code using it.

Another approach in 1.4.0 (real soon now) is to use type.convert, and
check if the answer is mode "numeric").

>
> 2. !as.logical(gsub("1","T",gsub("-1","F",as.character(regexpr("[^0-9]","123")))))
>    This responds "TRUE" for the string "123" having only numeric characters.
>    However, notice how harsh this is on the reader.

Well, numbers can have decimal points in, and you are only testing if
any character is non-numeric.

regexpr("[^\.0-9]","123") == -1

would be pretty good.  This would not allow exponential notation nor Inf
or -Inf, though.

>    Unfortunately, "regexpr" here responds in -1 and 1 rather than FALSE and TRUE,
>    so this becomes an extra verbose appoach.

See above.

> My question: CAN ONE BETTER DETECT NUMERIC DATA IN A CHARACTER VARIABLE?
> One first imagines trying,
>    is.numeric("123")
> but this responds FALSE, telling us merely that this is a character string.

Correct, as documented.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list