Home

How to Name Matrix Rows and Columns in R programming

|
|  Updated:  
2021-11-04 18:50:43
|   From The Book:  
Statistical Analysis with R Essentials For Dummies
Explore Book
Buy On Amazon
The rbind() function in the R programming language conveniently adds the names of the vectors to the rows of the matrix. You name the values in a vector, and you can do something very similar with rows and columns in a matrix.

For that, you have the functions rownames() and colnames(). Guess which one does what? Both functions work much like the names() function you use when naming vector values.

Changing the row and column names

The matrix baskets.team already has some row names. It would be better if the names of the rows would just read Granny and Geraldine. You can easily change these row names like this:
> rownames(baskets.team) <- c(“Granny”, “Geraldine”)
You can look at the matrix to check if this did what it’s supposed to do, or you can take a look at the row names itself like this:
> rownames(baskets.team)
[1] “Granny”  “Geraldine”
The colnames() function works exactly the same. You can, for example, add the number of the game as a column name using the following code:
> colnames(baskets.team) <- c(“1st”, “2nd”, “3th”, “4th”, “5th”, “6th”)
This gives you the following matrix:
> baskets.team
     1st 2nd 3th 4th 5th 6th
Granny   12  4  5  6  9  3
Geraldine  5  4  2  4 12  9
This is almost like you want it, but the third column name contains an annoying writing mistake. No problem there; R allows you to easily correct that mistake. Just as the with names() function, you can use indices to extract or to change a specific row or column name. You can correct the mistake in the column names like this:
> colnames(baskets.team)[3] <- “3rd”
If you want to get rid of either column names or row names, the only thing you need to do is set their value to NULL. This also works for vector names, by the way. You can try that out yourself on a copy of the matrix baskets.team like this:
> baskets.copy <- baskets.team
> colnames(baskets.copy) <- NULL
> baskets.copy
     [,1] [,2] [,3] [,4] [,5] [,6]
Granny   12  4  5  6  9  3
Geraldine  5  4  2  4  12  9

R stores the row and column names in an attribute called dimnames. Use the dimnames() function to extract or set those values.

Using names as indices

These row and column names can be used just like you use names for values in a vector. You can use these names instead of the index number to select values from a vector. This works for matrices as well, using the row and column names.

Say you want to select the second and the fifth game for both ladies; try:

> baskets.team[, c(“2nd”, “5th”)]
     2nd 5th
Granny   4  9
Geraldine  4 12
Exactly as before, you get all rows if you don’t specify which ones you want. Alternatively, you can extract all the results for Granny like this:
> baskets.team[“Granny”, ]
1st 2nd 3rd 4th 5th 6th
 12  4  5  6  9  3
That’s the result, indeed, but the row name is gone now. R tries to simplify the matrix to a vector, if that’s possible. In this case, a single row is returned so, by default, this result is transformed to a vector.

If a one-row matrix is simplified to a vector, the column names are used as names for the values. If a one-column matrix is simplified to a vector, the row names are used as names for the vector. If you want to keep all names, you must set the argument drop to FALSE to avoid conversion to a vector.

About This Article

This article is from the book: 

About the book author:

Andrie de Vries is a leading R expert and Business Services Director for Revolution Analytics. With over 20 years of experience, he provides consulting and training services in the use of R.

Joris Meys is a statistician, R programmer and R lecturer with the faculty of Bio-Engineering at the University of Ghent.