Objects in R


Attributes

R裡面使用了OOP的概念,先參考網路上相關教學把物件觀念弄小懂可以看比較清楚。

既然是object就有Attributes(metadata),可以透過attributes()存取物件的屬性,如果一個物件不具有任何屬性則得到NULL,一些常見的attribute:

  • names, dimnames (維度名稱)
  • dimensions (例如矩陣、陣列)
  • class (例如Integer、Numeric)
  • 其他未被定義的屬性。

Dimensions

dim()ncol()nrow()可取得行列的大小。

Names

  • Name in list

    list的每個element可以有一個index label

    > x <- 1:3
    > names(x) <- c("a", "b", "c")
    > x
    a b c 
    1 2 3
    
  • Name in Matrix、Date frame

    dimnames()colnames(x)rownames(x)可取得行列的名稱(R-programin一書裡面說這兩個function只適用matrices,Data frame要用其他的函式,但3.3.1版本實測兩者皆可,所以記一種就好),實例參考上面Data frame。

results matching ""

    No results matching ""