R语言:mode和class的区别

 
###############################################
总结:
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中的数据类型可以不一致,所以没法返回一个类型代表多种元素类型
 
 
 
########################################
在R中,他并不提供直接访问存储在内存中的数据的方法,而是提供特定的数据结构(我们将之称为对象)
 
mode:表示对象在内存中的存储类型
基本数据类型'atomic' mode: 
numeric(Integer/double), complex, character和logical
递归的对象(recursive object):
'list' 或 'function' 
 
class:是一种抽象类型,或者理解为一种数据结构
他主要是用来给泛型函数(参考java中泛型的概念)识别参数用。
如下例,你可以看到数据在内存中是列表的形式存储,却被包装成数据框来操作。(数据框其实是一种特殊的列表)
例子1
  1. <- data.frame(V1=c(1,2))  
  2. class(d)    #"data.frame"
  3. mode(d)     #"list"
再看一个例子2
  1. 例子2.1
  2.  
  3. x1<-c(1,2,3)
  4. x2<-c(2,3,4)
  5. x3<-c(3,4,5)
  6. xmerge<-data.frame(x1,x2,x3)
  7. class(xmerge)   #dataframe
  8. mode(xmerge)   #list
  1.  
  2. 例子2.2
  3. >class(xmerge[1,])
  4. [1]"data.frame"
  5. > mode(xmerge[1,])
  6. [1]"list"
  7. > xmerge[1,]
  8. x1 x2 x3
  9. 1123
  1.  
  2. 例子2.3
  3.  
  4. > x1<-c(2,6,3)
  5. > x2<-c(3,7,4)
  6. > x3<-c(9,4,5)
  7. > xmerge<-data.frame(x1,x2,x3)
  8. >class(xmerge[,1])
  9. [1]"numeric"
  10. > mode(xmerge[,1])
  11. [1]"numeric"
  12. > xmerge[,1]
  13. [1]263
  14. > xmerge
  15. x1 x2 x3
  16. 1239
  17. 2674
  18. 3345
从例子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. = 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.  
  21. x51<-x5[,,1]
  22.  
  23. 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. #也就是说,;列表中的每一项都是列表
  8. #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.  
  5. >class(jj[[1]]) #mode返回值也是character
  6. [1]"character"
  7.  
  8. > jj[1]
  9. $name
  10. [1]"jos""xuan"
  11. >class(jj[1]) #mode返回值也是list
  12. [1]"list"
  13.  
  14.  
  15.  
  16. > jj[[2]]
  17.  
  18. [1]55000
  19.  
  20. >class(jj[[2]])
  21. [1]"numeric"
  22.  
  23. > jj[2]
  24.  
  25. $salary
  26. [1]55000
  27. >class(jj[2])
  28.  
  29. [1]"list"
  30.  
  31. > jj[[1]][1]
  32.  
  33. [1]"jos"
  34. >class(jj[[1]][1])
  35.  
  36. [1]"character"
  37.  
  38. > mode(jj[[1]][1])
  39. [1]"character"
  40.  
  41. > jj[[3]
  42.  
  43. [1] TRUE
  44.  
  45. > jj[3]
  46.  
  47. $union
  48. [1] TRUE
  49. >is.vector(jj[[1]])
  50. [1] TRUE
  51. >is.vector(jj[[2]])
  52. [1] TRUE
  53.  
  54. >is.numeric(jj[[1]])
  55. [1] FALSE
  56. >is.numeric(jj[[2]])
  57. [1] TRUE
  58.  
  59. >is.vector(jj[[3]])
  60. [1] TRUE
  61. >is.list(jj[1])
  62. [1] TRUE
  63.  
  64. >is.vector(jj[1])
  65. [1] TRUE
因为s.vector(jj[1])返回值都是T,这个结果似乎,不是很令人信服
于是,搜索?is.vector()
查看帮助说明之后,我们知道了,原来默认的情况下是any,对于任意的原生类型和list以及表达式,都会返回T
例子5
 
  1.  
  2. 例子5.1
  3. > x <- c(=1, b =2)
  4. > x
  5. a b
  6. 12
  7. >class(x)
  8. [1]"numeric"
  9. >is.vector(x)
  10. [1] TRUE
  11. >is.vector(x,"double")  
  12. #默认是双精度的,要是整型,要加L
  13. [1] TRUE
  14. >is.vector(x,"Integer")
  15. [1] FALSE
  16.  
  17. 例子5.2
  18. >is.vector(jj[[1]],"list")
  19. [1] FALSE
  20. >is.vector(jj[1],"list")
  21. [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).
  • 13
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值