属性
您已经来到了本课程的最后一个练习!恭喜您!我们用一个简单的问题收个尾。
属性是关于数据结构的一些额外元数据。最常见的属性包括:行名、列名、维度,以及类。您可以使用 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 金融入门
练习说明
- 已为您定义矩阵
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