[R] is.numeric()

Ole Christensen o.christensen at lancaster.ac.uk
Thu Aug 22 10:56:37 CEST 2002


Thank you Thomas for poiting out the different sorts of NA (which was
really the core of my problem). Thanks also to Patrick Burns, who
refered me to page 17 of the book ``S Poetry'' (available at :
http://www.i-way.co.uk/~pat/Spoetry ).


I still have a problem with vectorizing is.numeric() for a logical
vector :

vec <- c(TRUE, FALSE, NA, NA)
sapply(vec,FUN=is.numeric)
is.numeric(vec[2])
is.numeric(vec[3])


gives:

> vec <- c(TRUE, FALSE, NA, NA)
> sapply(vec,FUN=is.numeric)
[1] TRUE TRUE TRUE TRUE
> is.numeric(vec[2])
[1] FALSE
> is.numeric(vec[3])
[1] FALSE


Cheers Ole



platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
status                    
major    1                
minor    5.1              
year     2002             
month    06               
day      17               
language R 



Thomas Lumley wrote:
> 
> 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

-- 
Ole F. Christensen
Department of Mathematics and Statistics
Fylde College, Lancaster University 
Lancaster, LA1 4YF, England
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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