How to Use the dimnames() Function in R


Often you may want to set or retrieve the dimension names of a matrix or data frame in R.

The easiest way to do so is by using the dimnames() function from base R, which is designed to perform this exact task.

The dimnames() function uses the following basic syntax:

dimnames(x)

where:

  • x: Name of object in R

If you simply use dimnames(x) then this function will return the dimension names of the object you specify.

However you can also assign values to dimnames(x), which will set the dimension names of the object that you specify.

The following example shows how to use the dimnames() function in practice in several different scenarios.

Note: The dimnames() function comes built-in with base R so you do not need to install or load any external packages to use this function.

Example: How to Use the dimnames() Function in R

Suppose that we create a matrix named my_matrixr with four rows and five columns:

#create matrix
my_matrix <- matrix(1:20, nrow=4)

#view matrix
my_matrix

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

Notice that the matrix simply has integer numbers for the dimension names of both the rows and the columns.

We can use the dimnames() function to attempt to retrieve the dimension names of the matrix:

#get dimension names of matrix
dimnames(my_matrix)

NULL

This returns NULL since the matrix currently doesn’t have any dimension names.

We can then assign dimension names to the matrix by using the dimnames() function with the following syntax:

#specify names for rows and columns of matrix
dimnames(my_matrix) <- list(Rows=c('A', 'B', 'C', 'D'),
                            Cols=c('col1', 'col2', 'col3', 'col4', 'col5'))

#view updated matrix
my_matrix

    Cols
Rows col1 col2 col3 col4 col5
   A    1    5    9   13   17
   B    2    6   10   14   18
   C    3    7   11   15   19
   D    4    8   12   16   20

Notice that the matrix now has the names for both the rows and columns that we specified.

It’s worth noting that the names that provide in the list() function are the names that will appear in the header of the matrix for the rows and columns.

For example, we could instead use MyCols and MyRows:

#specify names for rows and columns of matrix
dimnames(my_matrix) <- list(MyRows=c('A', 'B', 'C', 'D'),
                            MyCols=c('col1', 'col2', 'col3', 'col4', 'col5'))

#view updated matrix
my_matrix

      MyCols
MyRows col1 col2 col3 col4 col5
     A    1    5    9   13   17
     B    2    6   10   14   18
     C    3    7   11   15   19
     D    4    8   12   16   20

Notice that the headers for the rows and columns now reflect these custom names.

Now that we have set the dimension names for the rows and columns, we can use the dimnames() function once more to actually retrieve these names:

#get dimension names of matrix
dimnames(my_matrix)

$MyRows
[1] "A" "B" "C" "D"

$MyCols
[1] "col1" "col2" "col3" "col4" "col5"

Notice that the dimnames() function now retrieves the names for the rows and the columns of the matrix.

The first vector returned represents the dimension names for the rows while the second vector returned represents the dimension names for the columns of the matrix.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use str_split in R
How to Use str_replace in R
How to Count Words in String in R
How to Convert a Vector to String in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *