開始使用免費開始

屬性(Attributes)

你來到本課程的最後一題了!恭喜你!讓我們用一題輕鬆的收尾。

屬性是加在資料結構上的一些額外中繼資料。最常見的屬性包含:列名與欄名、維度,以及類別。你可以使用 attributes() 函式,回傳你所傳入物件的屬性清單。若要存取特定屬性,可以使用 attr() 函式。

探索 cash 的屬性:

attributes(cash)

$names
[1] "company"   "cash_flow" "year"     

$row.names
[1] 1 2 3 4 5 6 7

$class
[1] "data.frame"

attr(cash, which = "names")

[1] "company"   "cash_flow" "year"     

本練習屬於課程

R for Finance 入門

檢視課程

練習說明

  • 已為你定義矩陣 my_matrix 與因子 my_factor
  • my_matrix 使用 attributes()
  • my_matrix 使用 attr(),回傳 "dim" 屬性。
  • my_factor 使用 attributes()

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# my_matrix and my_factor
my_matrix <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3)
rownames(my_matrix) <- c("Row1", "Row2")
colnames(my_matrix) <- c("Col1", "Col2", "Col3")

my_factor <- factor(c("A", "A", "B"), ordered = T, levels = c("A", "B"))

# attributes of my_matrix


# Just the dim attribute of my_matrix


# attributes of my_factor
編輯並執行程式碼