关于R中的mode()和class()的区别



在R中,他并不提供直接访问存储在内存中的数据的方法,而是提供特定的数据结构(我们将之称为对象)

mode:表示对象在内存中的存储类型
基本数据类型'atomic' mode: 
numeric(Integer/double), complex, character和logical
递归的对象(recursive object):
'list' 或 'function' 

class:是一种抽象类型,或者理解为一种数据结构
他主要是用来给泛型函数(参考java中泛型的概念)识别参数用。
如下例,你可以看到数据在内存中是列表的形式存储,却被包装成数据框来操作。(数据框其实是一种特殊的列表)
例子1
  1. d <- data.frame(V1=c(1,2))  
  2. class(d)    #"data.frame"
  3. mode(d)     #"list"
再看一个例子2
  1. 例子2.1

  2. x1<-c(1,2,3)
  3. x2<-c(2,3,4)
  4. x3<-c(3,4,5)
  5. xmerge<-data.frame(x1,x2,x3)
  6. class(xmerge)   #dataframe
  7. mode(xmerge)   #list

  1. 例子2.2
  2. > class(xmerge[1,])
  3. [1] "data.frame"
  4. > mode(xmerge[1,])
  5. [1] "list"
  6. > xmerge[1,]
  7. x1 x2 x3
  8. 1 1 2 3

  1. 例子2.3

  2. > x1<-c(2,6,3)
  3. > x2<-c(3,7,4)
  4. > x3<-c(9,4,5)
  5. > xmerge<-data.frame(x1,x2,x3)
  6. > class(xmerge[,1])
  7. [1] "numeric"
  8. > mode(xmerge[,1])
  9. [1] "numeric"
  10. > xmerge[,1]
  11. [1] 2 6 3
  12. > xmerge
  13. x1 x2 x3
  14. 1 2 3 9
  15. 2 6 7 4
  16. 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
  1. x1 = array(rep(1,6),dim=c(2,3))
  2. class(x1)    #matrix
  3. mode(x1)     #numeric
例子3.2
  1. x = array(rep("a",6),dim=c(2,3)) #矩阵是数组的二维特殊情形
  2. class(x) #matrix
  3. mode(x)  #character
例子3.3
  1. x5 = array(rep("a",9),dim=c(3,3,3))
  2. x5      
  3. , , 1
  4.      [,1] [,2] [,3]
  5. [1,] "a"  "a"  "a" 
  6. [2,] "a"  "a"  "a" 
  7. [3,] "a"  "a"  "a" 
  8. , , 2
  9.      [,1] [,2] [,3]
  10. [1,] "a"  "a"  "a" 
  11. [2,] "a"  "a"  "a" 
  12. [3,] "a"  "a"  "a" 
  13. , , 3
  14.      [,1] [,2] [,3]
  15. [1,] "a"  "a"  "a" 
  16. [2,] "a"  "a"  "a" 
  17. [3,] "a"  "a"  "a"        
  18. class(x5)  #"array"       数据结构是数组
  19. mode(x5)   #"character"

  20. x51<-x5[,,1]

  21. class(x51) #"matrix"
例子3.4
  1. gl(2,5)            #新建一个因子
  2. class(gl(2,5))     #"factor"    数据结构是因子
  3. 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
  1. xl = list(fruit=c("apple","banana","pear"),
  2.           price=c(1,1,1.5),
  3.           market=c("newabest"))
  4. class(xl)    # "list"           数据结构是列表
  5. mode(xl)     # "list"

  6. #也就是说,;列表中的每一项都是列表
  7. #class(xl$fruit) #"character"
例子4.1说明,列表中class和mode都是list
但是class(xl$fruit) 得到的是"character"

先说明下
1list[[index]] 得到的是元素(组件)
2list[index]   得到的是子列表 这点请见《R语言经典实例》P119-121
  1. > jj<-list(name=c("jos","xuan"),salary=55000,union=T)
  2. > jj[[1]]
  3. [1] "jos" "xuan"

  4. > class(jj[[1]]) #mode返回值也是character
  5. [1] "character"

  6. > jj[1]
  7. $name
  8. [1] "jos" "xuan"
  9. > class(jj[1]) #mode返回值也是list
  10. [1] "list"



  11. > jj[[2]]

  12. [1] 55000

  13. > class(jj[[2]])
  14. [1] "numeric"

  15. > jj[2]

  16. $salary
  17. [1] 55000
  18. > class(jj[2])

  19. [1] "list"

  20. > jj[[1]][1]

  21. [1] "jos"
  22. > class(jj[[1]][1])

  23. [1] "character"

  24. > mode(jj[[1]][1])
  25. [1] "character"

  26. > jj[[3]

  27. [1] TRUE

  28. > jj[3]

  29. $union
  30. [1] TRUE
  31. > is.vector(jj[[1]])
  32. [1] TRUE
  33. > is.vector(jj[[2]])
  34. [1] TRUE

  35. > is.numeric(jj[[1]])
  36. [1] FALSE
  37. > is.numeric(jj[[2]])
  38. [1] TRUE


  39. > is.vector(jj[[3]])
  40. [1] TRUE
  41. > is.list(jj[1])
  42. [1] TRUE

  43. > is.vector(jj[1])
  44. [1] TRUE

因为s.vector(jj[1])返回值都是T,这个结果似乎,不是很令人信服
于是,搜索?is.vector()


查看帮助说明之后,我们知道了,原来默认的情况下是any,对于任意的原生类型和list以及表达式,都会返回T
例子5

  1. 例子5.1
  2. > x <- c(a = 1, b = 2)
  3. > x
  4. a b
  5. 1 2
  6. > class(x)
  7. [1] "numeric"
  8. > is.vector(x)
  9. [1] TRUE
  10. > is.vector(x,"double")  
  11. #默认是双精度的,要是整型,要加L
  12. [1] TRUE
  13. > is.vector(x,"Integer")
  14. [1] FALSE


  15. 例子5.2
  16. > is.vector(jj[[1]],"list")
  17. [1] FALSE
  18. > is.vector(jj[1],"list")
  19. [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中的数据类型可以不一致,所以没法返回一个类型代表多种元素类型








posted @ 2016-03-20 14:00  旅鼠  阅读(10549)  评论(0编辑  收藏  举报