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對象。
      ############################################
    


免責聲明!

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



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