在R中,他並不提供直接訪問存儲在內存中的數據的方法,而是提供特定的數據結構(我們將之稱為對象)
mode:表示對象在內存中的存儲類型
基本數據類型'atomic' mode:
numeric(Integer/double), complex, character和logical
遞歸的對象(recursive object):
'list' 或 'function'
class:是一種抽象類型,或者理解為一種數據結構
他主要是用來給泛型函數(參考java中泛型的概念)識別參數用。
如下例,你可以看到數據在內存中是列表的形式存儲,卻被包裝成數據框來操作。(數據框其實是一種特殊的列表)
例子1
d <- data.frame(V1=c(1,2))
class(d) #"data.frame"
mode(d) #"list"
再看一個例子2
例子2.1
x1<-c(1,2,3)
x2<-c(2,3,4)
x3<-c(3,4,5)
xmerge<-data.frame(x1,x2,x3)
class(xmerge) #dataframe
mode(xmerge) #list
例子2.2
> class(xmerge[1,])
[1] "data.frame"
> mode(xmerge[1,])
[1] "list"
> xmerge[1,]
x1 x2 x3
1 1 2 3
- 例子2.3
> x1<-c(2,6,3)
> x2<-c(3,7,4)
> x3<-c(9,4,5)
> xmerge<-data.frame(x1,x2,x3)
> class(xmerge[,1])
[1] "numeric"
> mode(xmerge[,1])
[1] "numeric"
> xmerge[,1]
[1] 2 6 3
> xmerge
x1 x2 x3
1 2 3 9
2 6 7 4
3 3 4 5
從例子1得知,數據框的class是dataframe,而其mode是list,例子2.1加強了這個結論。
例子2.2 說明數據框的每一行class仍然是data.frame,而其mode是list,即每一行仍然是一個數據框。(因為每一行會含有不同的數據類型)
例子2.3說明數據框的每一列的class和mode都是numeric(因為列的數據類型相同,而且我們沒有vector類型的說法)
例子3.1
x1 = array(rep(1,6),dim=c(2,3))
class(x1) #matrix
mode(x1) #numeric
例子3.2
x = array(rep("a",6),dim=c(2,3)) #矩陣是數組的二維特殊情形
class(x) #matrix
mode(x) #character
例子3.3
x5 = array(rep("a",9),dim=c(3,3,3))
x5
, , 1
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "a"
[3,] "a" "a" "a"
, , 2
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "a"
[3,] "a" "a" "a"
, , 3
[,1] [,2] [,3]
[1,] "a" "a" "a"
[2,] "a" "a" "a"
[3,] "a" "a" "a"
class(x5) #"array" 數據結構是數組
mode(x5) #"character"
x51<-x5[,,1]
class(x51) #"matrix"
例子3.4
gl(2,5) #新建一個因子
class(gl(2,5)) #"factor" 數據結構是因子
mode(gl(2,5)) #"numeric"
而例子3.1-3.4 說明一個問題:
class返回的是matrix,array,factor之類的數據結構類型。(在class中的參數是一個向量或者單個值的時候,返回值和mode相同,不會返回vector,不過可以使用is.vector()返回值是T驗證其確實是向量)
而mode返回的,是每一個元素的類型。
既然你說是每個元素的類型,那么為什么dataframe中mode返回的是list呢?
=============================================
例子4.1
xl = list(fruit=c("apple","banana","pear"),
price=c(1,1,1.5),
market=c("newabest"))
class(xl) # "list" 數據結構是列表
mode(xl) # "list"
#也就是說,;列表中的每一項都是列表
#class(xl$fruit) #"character"
例子4.1說明,列表中class和mode都是list
但是class(xl$fruit) 得到的是"character"
先說明下
1list[[index]] 得到的是元素(組件)
2list[index] 得到的是子列表 這點請見《R語言經典實例》P119-121
> jj<-list(name=c("jos","xuan"),salary=55000,union=T)
> jj[[1]]
[1] "jos" "xuan"
> class(jj[[1]]) #mode返回值也是
character[1] "character"
> jj[1]
$name
[1] "jos" "xuan"
> class(jj[1]) #mode返回值也是
list[1] "list"
> jj[[2]]
[1] 55000
> class(jj[[2]])
[1] "numeric"
> jj[2]
$salary
[1] 55000
> class(jj[2])
[1] "list"
> jj[[1]][1]
[1] "jos"
> class(jj[[1]][1])
[1] "character"
> mode(jj[[1]][1])
[1] "character"
> jj[[3]
[1] TRUE
> jj[3]
$union
[1] TRUE
> is.vector(jj[[1]])
[1] TRUE
> is.vector(jj[[2]])
[1] TRUE
> is.numeric(jj[[1]])
[1] FALSE
> is.numeric(jj[[2]])
[1] TRUE
> is.vector(jj[[3]])
[1] TRUE
> is.list(jj[1])
[1] TRUE
> is.vector(jj[1])
[1] TRUE
因為s.vector(jj[1])返回值都是T,這個結果似乎,不是很令人信服
於是,
搜索?is.vector()


查看幫助說明之后,我們知道了,原來默認的情況下是any,對於任意的原生類型和list以及表達式,都會返回T
例子5
例子5.1
> x <- c(a = 1, b = 2)
> x
a b
1 2
> class(x)
[1] "numeric"
> is.vector(x)
[1] TRUE
> is.vector(x,"double")
#默認是雙精度的,要是整型,要加L
[1] TRUE
> is.vector(x,"Integer")
[1] FALSE
例子5.2
> is.vector(jj[[1]],"list")
[1] FALSE
> is.vector(jj[1],"list")
[1] TRUE
說明,jj[1]實質上是一種list,而非向量
再看?mode()這一句中幫助文檔的寫法
Description
Get or set the type or storage mode of an object.
value
a character string giving the desired mode or ‘storage mode’ (type) of the object.
mode(x) <- "newmode"(修改x的數據類型為新的類型newmode)
changes the mode of object x to newmode. This is only supported if there is an appropriate as.newmode function, for example "logical", "integer", "double", "complex", "raw", "character", "list", "expression", "name", "symbol" and "function". Attributes are preserved (but see below).
###############################################
總結:
mode:表示對象在內存中的存儲類型
基本數據類型'atomic' mode:
numeric(Integer/double), complex, character和logical
遞歸的對象(recursive object):
'list' 或 'function'
class:是一種抽象類型,或者理解為一種數據結構(數據框,因子,列表)
他主要是用來給泛型函數(參考java中泛型的概念)識別參數用。
所以當給函數傳參數的時候如果發生錯誤,就查看class屬性
class返回的是matrix,array,factor(還在Date)之類的數據結構類型
class(x)
(一)
基本類型情況(參考java中的基本數據類型理解)
(1)當x是單個值,或者向量的時候,
返回的結果和mode一致,如numeric,character
(二)Object類型情況(參考java中引用數據類型情況理解)
(2)其他情況(矩陣,數組,日期,因子),
class返回(matrix,array,Date,factor)
mode返回(x中元素的類型——在內存中的存儲類型)
(3)當x是數據框的時候,class返回dataframe,mode返回list
當x是列表的時候,
class和mode都返回list
(4)當x是列表的時候,class和mode都返回list
為何數據框和列表不再適用於第二條mode返回x中元素的類型了呢?
1
因為數據框其實是列表的一種特殊情況
2list和dataframe中的數據類型可以不一致,所以沒法返回一個類型代表多種元素類型