[R] Detecting numerical value in character variable

Jameson C . Burt jameson at monumental.com
Wed Dec 5 23:00:45 CET 2001

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
      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.

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.
   Unfortunately, "regexpr" here responds in -1 and 1 rather than FALSE and TRUE, 
   so this becomes an extra verbose appoach.

One first imagines trying,
but this responds FALSE, telling us merely that this is a character string.

This problem arises in an R program I have used for years to balance my checkbook,
producing 5 lines identical to my bank's statement.
I input my checkbook data from a file with one natural column having entries like
(excluding # comments),
   3117             #check number
   SALARY:10-1-01   #salary deposited on 10/1/2001
   TRANSF:10-23-01  #transfer between accounts on 10/23/2001
These non-numerical descriptive entries speed balancing my checkbook,
especially when I error.

Jameson C. Burt, NJ9L   Fairfax, Virginia, USA
jameson at coost.com       http://www.coost.com
(202) 690-0380 (work)
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