[R] is.numeric()

Ole Christensen o.christensen at lancaster.ac.uk
Wed Aug 21 14:52:56 CEST 2002


Dear R-list

I am having troubles understanding how the function is.numeric() works.
Any help appreciated.

Some commands are given here (the output and my comments are given
below)


vec <- c(1.4, NA, NA, NA)
sapply(vec,FUN=is.numeric)
is.numeric(vec[2])
is.na(vec[2])
is.numeric(NA)

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

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


###
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() ?


> 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
> is.numeric(FALSE)
[1] FALSE


Here I am surprised that is.numeric(vec[2]) doesn't return the same
result as in the first example. The element is NA in both cases.

Apparently my attempt to vectorize is.numeric doesn't give what I
expected. I would have expected that 
sapply(vec,FUN=is.numeric) would equal
c(is.numeric(vec[1],is.numeric(vec[2],is.numeric(vec[3],is.numeric(vec[4])



Cheers Ole


> version
         _                
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  



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