2016年6月9日 星期四

Swirl_01-RBasic-03-Data-Structure-Object

01-RBasic-03-Data-Structure-Object
-----------------------------------------------


01. 向量物件有兩個很重要的屬性:`mode`和`length`。
    - `mode`代表這個向量的值型態,總共有:    `logical`、`integer`、`numeric`、`complex`、`character`和`raw`。
    - `length`代表這個向量的值的個數。

02. `[ ]`和`[[ ]]`的差異。單層方括弧`[ ]`,R 取出向量的元素並且維持原有屬性,除了傳回元素值外也傳回names與其他屬性。使用雙層方括弧`[[ ]]`時,R 會只傳回元素值。

03. list向量最有用的地方,在於它可以裝不同型態的值。包含不同屬性、不同長度的數值、文字、向量甚至是矩陣。

04. 所有的R 物件,都有「屬性」(attributes)。我們可以用`attributes`這個函數來印出一個R 物件的屬性與屬性內容。
    >  attributes(g)
    $names
     [1] "coefficients"  "residuals"     "effects"       "rank"          "fitted.values" "assign"      
     [7] "qr"            "df.residual"   "xlevels"       "call"          "terms"         "model"       

    $class
    [1] "lm"

05. `names(g)`來查詢`g`的名字
    > names(g)
     [1] "coefficients"  "residuals"     "effects"       "rank"          "fitted.values" "assign"      
     [7] "qr"            "df.residual"   "xlevels"       "call"          "terms"         "model"       
    也可以用`attr(g, "names")`來取得名字是"names"的屬性
    > attr(g, "names")
     [1] "coefficients"  "residuals"     "effects"       "rank"          "fitted.values" "assign"      
     [7] "qr"            "df.residual"   "xlevels"       "call"          "terms"         "model"       

06. `class(g)`來查詢`g`的類別
    > class(g)
    [1] "lm"

   
07. 了解像attributes這樣的功能,在探索一些複雜的R模型物件時,是非常好用的。我們在之後深入探討`matrix`、`data.frame`等進階物件的時候, 也會需要了解attributes的相關的知識。而名稱為"class"的屬性是非常重要的,因為它牽涉到R的物件導向設計。


沒有留言:

張貼留言