R语言-数据类型、结构以及对象类型3

R语言-数据类型|数据结构|对象类型

  R是一种基于对象(Object)的语言,所以你在R语言中接触到的每样东西都是一个对象,一串数值向量是一个对象,一个函数是一个对象,一个图形也是一个对象。基于对象的编程(OOP)就是在定义类的基础上,创建与操作对象。
  对象中包含了我们需要的数据,同时对象也具有很多属性(Attribute)。其中一种重要的属性就是它的类(Class),R语言中最为基本的类包括了数值(numeric)、逻辑(logical)、字符(character)、列表(list),在此基础上构成了一些复合型的类,包括矩阵(matrix)、数组(array)、因子(factor)、数据框(dataframe)。除了这些内置的类外还有很多其它的,用户还可以自定义新的类,但所有的类都是建立在这些基本的类之上的。
	#常用函数
	class(x)
	typeof(x)
	mode(x)
	storage.mode(x)
	attributes(x)
	str(x)
	unclass(x)
  • 函数 class 返回R对象的类(基本类|复合型的类|其他类(lm函数)|自定义类),如果要消除对象的类则可用unclass()。

      class()
      unclass()
    
  • R语言特有的函数 typeof 返回R对象的类型。注意在 R 底层的 C 代码中,所有对象都是指向一个有类型定义 SEXPREC的结构体(structure)的指针;不同的R数据类型在 C 里面用 决定结构体各部分信息的 SEXPTYPE 表示。

      typeof()
    
    typeof 可能的返回值和涵义
    返回值 涵 义
    NULL
    symbol 一个变量名字
    pairlist 成对列表对象
    closure 一个函数
    environment 一个环境
    promise 一个用于实现悠闲赋值的对象
    language 一个 R 语言构建
    special 一个不可针对参数求值的内置函数
    builtin 一个可针对参数求值的内置函数
    logical 含逻辑值的向量
    integer 含整数值的向量
    double 含实数值的向量
    complex 含复数值的向量
    character 含字符值的向量
    ... 特定变量长度参数 ***
    any 一个可以匹配任何类型的特殊类型 ***
    expression 一个表达式对象
    list 一个列表
    externalptr 一个外表指针对象
    weakref 一个弱引用对象(a weak reference object)
    raw 一个字节元素向量

我认为用户不用深入以'***'标记的条目,至少没有想象的那么容易, 但是可以多看一些例子。

R 在计算过程中,对象常常需要强制转换成不同的类型(type)。 有许多函数可用于显式的强制转换。 在仅仅用 R 语言编程的时候,一个对象的类型通常不会影响计算结果, 但是当混合使用外部编程语言或不同的操作系统时, 常常需要保证对象类型的正确。

  • 函数 mode 返回对象的模式信息

      mode()
    
  • 函数storage.mode返回其参数的存储模式(storage mode)

      attributes()
    
  • 举例1

      > 
      > x <- 1:3
      > class(x)
      [1] "integer"
      > typeof(x)
      [1] "integer"
      > mode(x)
      [1] "numeric"
      > storage.mode(x)
      [1] "integer"
      > unclass(x)
      [1] 1 2 3
      > attributes(x)
      NULL
      > str(x)
       int [1:3] 1 2 3
      >
      ##说明:
      ##如果运行attributes(x),会发现返回了空值。
      ##这是因为x是一个向量,对于向量这种内置的基本类,attributes是没有什么好显示的。
    
  • 举例2

      > 
      > x <- runif(10) 
      > y <- rnorm(10)+10*x
      > model <- lm(y ~ x)
       
      > class(model)
      [1] "lm"
    
      > typeof(model)
      [1] "list"
    
      > mode(model)
      [1] "list"
    
      > storage.mode(model)
      [1] "list"
    
      > unclass(model)
      $coefficients
      (Intercept)           x 
        -0.701304   10.104148 
    
      $residuals
                1           2           3           4           5 
       0.98690445 -0.86301894  0.04973016 -1.32000648  0.57173416 
                6           7           8           9          10 
      -0.66068807  1.39675119 -0.08431814 -0.31170431  0.23461598 
    
      $effects
      (Intercept)           x                                     
      -14.5796672  -7.6233656  -0.1266812  -1.2665738   0.4738679 
                                                                  
       -1.0809453   1.7945956   0.2192125  -0.0512103  -0.2297554 
    
      $rank
      [1] 2
      
      $fitted.values
             1        2        3        4        5        6        7 
      0.569911 3.881950 4.073688 5.717006 4.635263 2.330260 8.179452 
             8        9       10 
      7.505135 7.197435 2.014856 
      
      $assign
      [1] 0 1
    
      $qr
      $qr
         (Intercept)           x
      1   -3.1622777 -1.66242471
      2    0.3162278 -0.75447882
      3    0.3162278  0.05692437
      4    0.3162278  0.27248772
      5    0.3162278  0.13058931
      6    0.3162278 -0.17177094
      7    0.3162278  0.59550060
      8    0.3162278  0.50704667
      9    0.3162278  0.46668400
      10   0.3162278 -0.21314423
      attr(,"assign")
      [1] 0 1
      
      $qraux
      [1] 1.316228 1.031773
      
      $pivot
      [1] 1 2
      
      $tol
      [1] 1e-07
      
      $rank
      [1] 2
      
      attr(,"class")
      [1] "qr"
      
      $df.residual
      [1] 8
      
      $xlevels
      named list()
      
      $call
      lm(formula = y ~ x)
      
      $terms
      y ~ x
      attr(,"variables")
      list(y, x)
      attr(,"factors")
        x
      y 0
      x 1
      attr(,"term.labels")
      [1] "x"
      attr(,"order")
      [1] 1
      attr(,"intercept")
      [1] 1
      attr(,"response")
      [1] 1
      attr(,".Environment")
      <environment: R_GlobalEnv>
      attr(,"predvars")
      list(y, x)
      attr(,"dataClasses")
              y         x 
      "numeric" "numeric" 
      
      $model
                y         x
      1  1.556815 0.1258112
      2  3.018931 0.4536012
      3  4.123418 0.4725774
      4  4.397000 0.6352154
      5  5.206997 0.5281560
      6  1.669572 0.3000316
      7  9.576203 0.8789218
      8  7.420817 0.8121851
      9  6.885731 0.7817324
      10 2.249472 0.2688164
      
    
      > attributes(model)
      $names
       [1] "coefficients"  "residuals"     "effects"      
       [4] "rank"          "fitted.values" "assign"       
       [7] "qr"            "df.residual"   "xlevels"      
      [10] "call"          "terms"         "model"        
      
      $class
      [1] "lm"
    
    
    
      > str(model)
      List of 12
       $ coefficients : Named num [1:2] -0.701 10.104
        ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
       $ residuals    : Named num [1:10] 0.9869 -0.863 0.0497 -1.32 0.5717 ...
        ..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
       $ effects   
      ##内容太多次数省略
      
      ############################################
      ##说明:
      ##可以看到这个对象的类是“lm”,这意味着什么呢?我们知道对于不同的类有不同的处理方法,那么对于model这个对象,就有专门用来处理lm类对象的函数,
      ##例如plot.lm()。但如果你用普通的函数plot()也一样能显示其图形,
      ##Why?因为plot()这种函数会自动识别对象的类,从而选择合适的函数来对付它,这种函数就称为泛型函数(generic function)。
      ##你可以用methods(class=lm)来了解有哪些函数可适用于lm对象。
      ############################################
    
posted @ 2016-06-13 11:15  银河统计  阅读(6239)  评论(0编辑  收藏  举报