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

The rownames() method in R is used to set the names to rows of a matrix. The colnames() method in R is used to set the names to columns of a matrix. But these functions don’t operate on both rows and columns at once, but dimnames() can. Let’s see dimnames() function in detail.

dimnames() Function in R

The dimnames() is a built-in R function that can set or get the row and column names of R Objects. The dimnames() function accepts an R object like matrix, array, or data frame.

The dimnames() function operates on both rows and columns at once. If you use it to set the names, you need to specify the names for the rows and columns in that order) in a list.

Syntax

dimnames(x)

Arguments

x: It is an R object like a matrix, array, or data frame.

Example

Let’s define a Matrix and get the dimensions using the dimnames() function.

m1 <- matrix(1:20, nrow = 4)
m1

Output

     [,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

Now, we can set the column names using the dimnames() function.

m1 <- matrix(1:20, nrow = 4)
dimnames(m1) <- list(month.abb[1:4], month.abb[5:9])
print(m1)

Output

     May Jun Jul Aug Sep
Jan   1   5   9  13  17
Feb   2   6  10  14  18
Mar   3   7  11  15  19
Apr   4   8  12  16  20

Now, let’s get the names using the dimnames() function. For a 1D matrix, the names are the same as the (only) component of the dimnames.

m1 <- matrix(1:20, nrow = 4)
dimnames(m1) <- list(month.abb[1:4], month.abb[5:9])
dimnames(m1)

Output

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

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

Applying dimnames() function to a data frame

To create a data frame in R, use the data.frame() function.

streaming <- data.frame(service_id = c(1:5),
                        service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
                        service_price = c(18, 10, 15, 7, 12),
                        stringsAsFactors = FALSE)

The data.frame() function creates data frames, tightly coupled collections of variables that share many properties of matrices and lists, used as the fundamental data structure.

Now, we will pass this data frame to the dimnames() function.

streaming <- data.frame(service_id = c(1:5),
service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
service_price = c(18, 10, 15, 7, 12),
stringsAsFactors = FALSE)

dimnames(streaming)

Output

[[1]]
[1] "1" "2" "3" "4" "5"

[[2]]
[1] "service_id" "service_name" "service_price"

You can see that the rownames are 1, 2, 3, 4, 5, and colnames are service_id, service_name, service_price.

Applying dimnames() function to an Array

An Array in R is a list or vector with two or more dimensions. An array is like a stacked matrix, and a matrix is a two-dimensional array. To create an array, use the array() method.

rv <- c(11, 19, 21)
rv2 <- c(6, 29, 46, 37, 38)

# Take these vectors as input to the array.
result <- array(c(rv, rv2), dim = c(3, 3, 2))
print(result)

Output

, , 1

     [,1] [,2] [,3]
[1,]  11    6   37
[2,]  19   29   38
[3,]  21   46   11

, , 2

     [,1] [,2] [,3]
[1,]  19   29   38
[2,]  21   46   11
[3,]   6   37   19

Now, we will pass this array to the dimnames() function.

rv <- c(11, 19, 21)
rv2 <- c(6, 29, 46, 37, 38)

# Take these vectors as input to the array.
result <- array(c(rv, rv2), dim = c(3, 3, 2))
dimnames(result)

Output

NULL

Here, the output is NULL.

Conclusion

The dimnames() function returns or changes the dimnames attribute of an array or data frame. This is a list of the same length as the dimension of the array or data frame.

That is it for the dimnames() function in R.

Leave a Comment