Dimnames in R (dimension name) is an attribute of the object. The dimname can be either NULL or a list of the same length as the array, matrix, or the number of rows and columns of the dataframe. This article will share what dimnames in R is and how to use it to set and retrieve dimension names of objects.
What is the dimnames in R?
The dimnames() function in R is used to set or retrieve the dimension name of an object.
Syntax:
# Retrieve the dimnames
dimnames(x)
# Set the dimnames
dimnames(x) <- value
Parameters:
- x: an R object
- value: value for dimnames(x)
For the dataframe, the dimnames are row names and their names. Row names must be unique/distinct.
How to use the dimnames() function in R?
We will give examples of using the dimnames() function in R to retrieve or set the dimnames of an object.
We create a matrix with 3 rows and 3 columns containing the integers 1 to 9 and a simple dataframe containing some students’ height and weight measurements.
Example:
# Create a matrix cat("\nThe Matrix\n") intMatrix <- matrix(1:9, nrow = 3) print(intMatrix) # Create a data frame cat("\nThe Data Frame\n") student <- data.frame( height = c(165, 178, 168, 173, 170), weight = c(55, 72, 65, 60, 56) ) print(student)
Output:
The Matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
The Data Frame
height weight
1 165 55
2 178 72
3 168 65
4 173 60
5 170 56
Retrieve the dimnames of an object
First, we use the dimnames() function to display a list of dimension names of the objects.
Example:
intMatrix <- matrix(1:9, nrow = 3) student <- data.frame( height = c(165, 178, 168, 173, 170), weight = c(55, 72, 65, 60, 56) ) # Display dimnames of the matrix object cat("Dimnames of the matrix object\n") matrixDimnames <- dimnames(intMatrix) print(matrixDimnames) # Display dimnames of the data frame object cat("\nDimnames of the data frame object\n") studentDimnames <- dimnames(student) print(studentDimnames)
Output:
Dimnames of the matrix object
NULL
Dimnames of the data frame object
[[1]]
[1] "1" "2" "3" "4" "5"
[[2]]
[1] "height" "weight"
Since the ‘intMatrix’ matrix has no column and row names, this matrix’s dimnames() function returns NULL. The dimnames() function call on the ‘student’ dataframe returns a list of row names by default of integers from 1 and a previously generated list of column names.
Set the dimnames of an object
Next, we’ll use the dimnames() function to set new column and row names for the created objects. As a first step, we create a list object containing the new row and column names for the object. We only give the new row name for the data frame object because the column name is already set.
Example:
intMatrix <- matrix(1:9, nrow = 3) student <- data.frame( height = c(165, 178, 168, 173, 170), weight = c(55, 72, 65, 60, 56) ) cat("Dimnames for the matrix object\n") matrixDimnames <- list( c("row1", "row2", "row3", "row4"), c("col1", "col2", "col3", "col4") ) print(matrixDimnames) cat("\nNew row names of the data frame object\n") studentRowNames <- c("Rebecca", "Alana", "Dudley", "Oliver", "Laura") print(studentRowNames)
Output:
Dimnames for the matrix object
[[1]]
[1] "A" "B" "C"
[[2]]
[1] "A" "B" "C"
New row names of the data frame object
[1] "Rebecca" "Alana" "Dudley" "Oliver" "Laura"
Now, we use the dimnames() function to give the rows and columns new names.
Example:
intMatrix <- matrix(1:9, nrow = 3) student <- data.frame( height = c(165, 178, 168, 173, 170), weight = c(55, 72, 65, 60, 56) ) matrixDimnames <- list( c("A", "B", "C"), c("A", "B", "C") ) studentRowNames <- c("Rebecca", "Alana", "Dudley", "Oliver", "Laura") cat("Set dimnames for the matrix object\n") dimnames(intMatrix) <- matrixDimnames print(intMatrix) cat("\nSet new row names for data frame object\n") dimnames(student)[[1]] <- studentRowNames print(student)
Output:
Set dimnames for the matrix object
A B C
A 1 4 7
B 2 5 8
C 3 6 9
Set new row names for data frame object
height weight
Rebecca 165 55
Alana 178 72
Dudley 168 65
Oliver 173 60
Laura 170 56
Alternatively, you can use the provideDimnames() function to set the dimension names for the object. This function will set all NULL dimension names to the specified base strings.
To know more about the syntax and parameters of the provideDimnames() function, click here.
Example:
intMatrix <- matrix(1:9, nrow = 3) cat("Set dimnames for the matrix object\n") provideMatrix <- provideDimnames(intMatrix, base = list(LETTERS)) print(provideMatrix)
Output:
Set dimnames for the matrix object
A B C
A 1 4 7
B 2 5 8
C 3 6 9
In the above example, we used the provideDimnames() function to set all the NULL dimension names to uppercase alphabetic characters.
Summary
In this article, we shared how to use the dimnames() function in R to set and retrieve the dimension names of an object. You can also use the provideDimnames() function to automatically set all dimension names to NULL to the specified base strings. Thank you for reading.
Maybe you are interested:
- The mean() Function In R: How To Use mean() In R
- Confidence Interval In R – How To Calculate The Confidence Interval In R
- cbind function in r

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP