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

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

2 weeks ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

2 weeks ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

3 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

4 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

2 months ago