numeric與double的區別


關於一個type, 有numeric(創建), as.numeric(轉換)和is.numeric(判斷)這三個函數. 所以numeric與double 來看看這三類函數表現有什么區別.

numeric與double函數沒有區別. as.numeric與as.double也沒有區別. 但is.numeric和is.double就有區別了.

區別在於: is.numeric在參數的typeof是integer和double都會返回T. 而is.double則只有在base是double的情況下才返回T, integer為base是會返回FALSE的.

比如:

> a=c(1L,2L)
> is.numeric(a)
[1] TRUE
> is.double(a)
[1] FALSE

對於list, 它的typeof本來就不是integer等, 所以即使元素的確都是numeric, is.numeric還是會得到FALSE

> l=list(a=1,b=2)
> is.numeric(l)
[1] FALSE

從下面可以看出: 判斷的是base, 而不一定是向量.

> d=Sys.Date() 
> class(d)
[1] "Date"
> typeof(d)                                                                                           
[1] "double"                                                                                          
> d                                                                                                   
[1] "2020-10-13"                                                                                      
> is.double(d)                                                                                        
[1] TRUE
不解的地方

文檔說integer vectors的class包括c("integer", "numeric")

Most vectors have class the result of mode(x), except that integer vectors have class c("integer", "numeric") and real vectors have class c("double", "numeric").

> x=integer(3)
> class(x)
[1] "integer"

class並不包括numeric


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM