[R] is.numeric()

Thomas Lumley tlumley at u.washington.edu
Wed Aug 21 17:28:01 CEST 2002


On Wed, 21 Aug 2002, Ole Christensen wrote:

> ###
> output + comments :
> ###
>
>
> > vec <- c(1.4, NA, NA, NA)
> > sapply(vec,FUN=is.numeric)
> [1] TRUE TRUE TRUE TRUE
> > is.numeric(vec[2])
> [1] TRUE
> > is.na(vec[2])
> [1] TRUE
> > is.numeric(NA)
> [1] FALSE
>
>
> I would have expected that is.numeric() on vec[2], vec[3] and vec[4]
> would give three times FALSE.
>
>
> My attempt to try to vectorize is.numeric() using sapply() might be
> nonsense. Is it possible to vectorize is.numeric() ?

You can vectorize anything.  You appear to be getting confused by the fact
that there is more than one sort of NA.

In the first case vec is a numeric vector, so the NAs in it are numeric
NAs. That is, they represent some number but you don't know which one
because you didn't measure it. This is numeric.

In the second case vec is a logical vector, so the NAs are logical NAs:
the value is TRUE or FALSE but you don't know which one.  This is not
numeric

You can also have character NAs, representing a missing character string.
This isn't numeric either.

The `default' NA produced when you type `NA' is a logical NA and so isn't
numeric.

There's also NaN, which represents the value of an undefined mathematical
operation, eg 0/0.

We need different types of NA because all the elements in a vector must be
of the same type.


	-thomas

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