12) Labeling a Matrix.

This Blog entry is from the Data Structures section in Learn R.

It is helpful for reference to label a Vector.  It is possible also to label the rows and the columns of a matrix in a similar fashion using the rownames() and colnames() function.

To set column names assign a Vector to the colnames() function, where the colnames() function accepts the matrix as its argument:

colnames(OverspillMatrix) <- c("Example1","Example2","Example3","Example4")
a-vector-of-column-names-in-r-script.png

Run the line of script to console:

a-vector-of-column-names-created-and-written-out-to-r-console.png

The rownames() function has a similar signature and takes a Vector of row names:

rownames(OverspillMatrix) <- c("Row1","Row2","Row3")
script-to-create-row-names-in-r.png

Run the line of script to console:

script-to-create-row-names-in-r-written-out-to-r-console.png

The matrix is now labelled in both directions and can be inspected by typing:

OverspillMatrix
script-to-confirm-that-a-matrix-is-now-names-on-columns-and-rows.png

Run the line of script to console:

a-fully-named-matrix-written-out-to-r-console.png