R如何为矩阵添加第二个标签

Mar*_*oli 4 r matrix

我想构建这个矩阵在此输入图像描述

我尝试了什么

table <- matrix(c(163,224,312,314,303,175,119,662,933,909,871,702,522,307,1513,2400,2164,2299,1824,1204,678,1603,2337,2331,2924,2360,1428,808,2834,3903,3826,4884,3115,2093,89), nrow=5, ncol=7, byrow=T)

rownames(table) <- c("Fair", "Good", "Very Good", "Premium", "Ideal")

 colnames(table) <- c("D", "E", "F", "G", "H", "I", "J")
Run Code Online (Sandbox Code Playgroud)

但结果如下:

在此输入图像描述

我的问题是如何添加colorcut标签

akr*_*run 6

dimnames(table)是一个'列表'.在原始矩阵"表"中,列表元素未命名.我们可以使用names将列表名称从"NULL"更改为首选名称.

names(dimnames(table)) <- c('cut', 'color')
table
#          color
# cut          D    E    F    G    H    I   J
#  Fair       163  224  312  314  303  175 119
#  Good       662  933  909  871  702  522 307
#  Very Good 1513 2400 2164 2299 1824 1204 678
#  Premium   1603 2337 2331 2924 2360 1428 808
#  Ideal     2834 3903 3826 4884 3115 2093  89
Run Code Online (Sandbox Code Playgroud)

注意: table是一个R函数,因此最好将对象命名为其他名称.