R Advanced

R dimnames() Function (With Matrix, Data Frame, and Array)

Dimensions are the structure of the data. A matrix has two dimensions: a row and a column, while an array can have multiple dimensions.

Dimension labels are names that we can assign to a dimension. For example, for a matrix, row names are labels, and column names are also.

The dimnames() function in R is used to get or set the names of an object’s dimensions, such as a matrix, data frame, or array.

Syntax

dimnames(obj)

Parameters

Name Value
obj It is an R object.

Setting the dimnames of the matrix

You can assign dimension names using dimnames(x) <- list(row_names, col_names). This sets the names for your object.

When setting dimension names, ensure that the length of the names matches the respective dimension of the data structure (e.g., the number of row names should match the number of rows).

Figure 1: Set the dimnames of a matrix

mat <- matrix(1:9, nrow = 3)

dimnames(mat) <- list(month.abb[1:3], month.abb[5:7])

print(mat)

Output

    May Jun Jul
Jan  1   4   7
Feb  2   5   8
Mar  3   6   9

Here, you can see that we assigned row names as well as column names using the dimnames() function with the help of list(). 

By default, indices represented row and column labels, but we changed this to month names.

Getting the dimnames of the matrix

To get the list of dimension names, use dimnames(x) (where x is your object.)

For a matrix, this list will have two elements: the first for row names and the second for column names.

mat <- matrix(1:9, nrow = 3)

dimnames(mat) <- list(month.abb[1:3], month.abb[5:7])

dimnames(mat)

Output

[[1]]
 [1] "Jan" "Feb" "Mar"

[[2]]
 [1] "May" "Jun" "Jul"

You can see that we have a list with two elements. First is row names. The second is column names.

You should use “double square brackets ([[ ]])” to access specific dimension names from the list.

Data Frame

If you use the dimnames() function with the data frame and want to get the dimensions, it will return a list of two elements: row names and column names.

df <- data.frame(
  col1 = c(1, 2, 3),
  col2 = c(4, 5, 6),
  col3 = c(7, 8, 9)
)

dimnames(df)

Output

[[1]]
[1] "1" "2" "3"

[[2]]
[1] "col1" "col2" "col3"

From the above program, you can see that we are getting the dimension names of the data frame.

We can also set the row and column names in the data frame.

df <- data.frame( col1 = c(1, 2, 3), col2 = c(4, 5, 6), col3 = c(7, 8, 9) ) 

print(df)

dimnames(df) <- list(c("R1", "R2", "R3"), c("C1", "C2", "C3"))

print("After changing row and column names")

print(df)

Output

Array

Arrays are different structures that can have more than two dimensions. They require a list with a length equal to the number of dimensions.

You can set the array dimensions using the “dimnames(main_arr) <- list(dim1, dim2, dim3)” syntax.

main_arr <- array(1:8, dim = c(2, 2, 2))

print(main_arr)

dim1 <- c("R1", "R2")
dim2 <- c("C1", "C2")
dim3 <- c("L1", "L2")

dimnames(main_arr) <- list(dim1, dim2, dim3)
print("After setting dimensions")

print(main_arr)

Output

You can see from the above output screenshot that we assigned dim1, dim2, and dim3 to the array, as highlighted in the image.

Recent Posts

How to Create an Empty Vector and Append Values in R

R vectors are atomic, which means they have homogeneous data types. They are contiguous in…

7 hours ago

How to Remove Single and Multiple Columns from Data Frame in R

DataFrames are like tables that contain rows and columns. Each column can have a different…

1 day ago

How to Convert Date to Numeric in R

Dates in R are stored as the number of days since 1970-01-01, so converting a…

2 days ago

How to Create a Data Frame from Vectors in R

In R, you can think of a vector as a series of values in a…

2 weeks ago

R dplyr::filter() Function: Complete Guide

The dplyr filter() function in R subsets a data frame and retains all rows that…

2 weeks ago

R distinct() Function from dplyr

The dplyr::distinct() function in R removes duplicate rows from a data frame or tibble and keeps…

2 weeks ago