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 Calculate Standard Error in R

Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…

3 days ago

R max() and min() Functions

max() The max() function in R finds the maximum value of a vector or data…

4 days ago

R as.Date() Function: Working with Dates

The as.Date() function in R converts various types of date and time objects or character…

1 week ago

R pnorm() Function [With Graphical Representation]

The pnorm() function in R calculates the cumulative density function (cdf) value of the normal…

1 week ago

Converting Vector to String in R

You may want to convert a vector to a string when you need to combine…

2 weeks ago

How to Set and Get Working Directory [setwd() and getwd()] in R

Set the current working directory The setwd() function sets the working directory to the new…

2 weeks ago