R how to add a second label for the matrix

I want to build this matrix enter image description here

What i tried

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")

but the result is this:

enter image description here

and my question is how to add tags colorandcut

+4
source share
1 answer

Here dimnames(table)is a "list". In the source table, "table" list items are not called. We can use namesto change list names from "NULL" to preferred.

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

NOTE. tableis a function R, so it's best to name the object by a different name.

+6
source

All Articles