[R] is.numeric

Arne Henningsen ahenningsen at email.uni-kiel.de
Thu Feb 20 10:59:03 CET 2003

I have a vector, which contains both strings and numbers, e.g.

> foo <- c("str1",1234,"str2",0.9876)

I want to know if a distinct element of the vector is a string or a number and 
took "is.numeric", but

> is.numeric(foo[2])

because R treats the numbers in a mixed vectors as strings:

> foo
[1] "str1"   "1234"   "str2"   "0.9876"

As a workaround I use:

> !is.na(as.numeric(foo[2]))
[1] TRUE
> !is.na(as.numeric(foo[1]))
Warning message:
NAs introduced by coercion

This works, but I always get the spurious warning messages. Do you know a 
better way or a way to suppress these warning messages?


Arne Henningsen
Department of Agricultural Economics
Christian-Albrechts-University Kiel
24098 Kiel, Germany
Tel: +49-431-880-4445
Fax: +49-431-880-1397 
ahenningsen at email.uni-kiel.de

More information about the R-help mailing list