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

R length(): Vector, List, Matrix, Array, Data Frame, String

Before executing an operation on an object, it is advisable to check its length, as…

15 hours ago

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…

2 days ago

Adding Single or Multiple Columns to Data Frame in R

Whether you want to add new data to your existing datasets or create new variables…

4 days ago

sqrt() Function: Calculate Square Root in R

The square root of a number is a value that is multiplied by itself, giving…

5 days ago

How to Remove Duplicate Rows from DataFrame in R

Duplicate rows refer to all the values across all columns that are the same in…

6 days ago

How to Remove NA From Vector in R

A vector is a data structure that holds the same type of data. When working…

1 week ago