Change Row Names of Data Frame or Matrix in R (4 Examples)
In this tutorial, I’ll illustrate how to modify the row names of a data frame or matrix in the R programming language.
The post consists of this:
Let’s dive into it.
Creation of Example Data
Have a look at the example data below:
data <- data.frame(x1 = 1:5, # Create data frame x2 = letters[1:5], x3 = 8) data # Print data frame # x1 x2 x3 # 1 1 a 8 # 2 2 b 8 # 3 3 c 8 # 4 4 d 8 # 5 5 e 8
Have a look at the previous RStudio console output. It shows that our example data has five rows and three columns. The row names are ranging from 1 to 5.
Example 1: Manually Specify Row Names of Data Frame
This section explains how to adjust each row name of our data frame manually by using the row.names function. Consider the following R syntax:
data1 <- data # Duplicate data frame row.names(data1) <- c(7, 1, 3, 8, 5) # Modify row names data1 # Print updated data frame # x1 x2 x3 # 7 1 a 8 # 1 2 b 8 # 3 3 c 8 # 8 4 d 8 # 5 5 e 8
As you can see, we have changed the row names of our data frame to a manually defined sequence of numbers.
Example 2: Using Letters as Row Names
This section illustrates how to name the rows of a data frame with alphabetical letters.
data2 <- data # Duplicate data frame row.names(data2) <- LETTERS[1:5] # Letters as row names data2 # Print updated data frame # x1 x2 x3 # A 1 a 8 # B 2 b 8 # C 3 c 8 # D 4 d 8 # E 5 e 8
Our new data frame has row names ranging from the letter A to the letter E.
Example 3: Changing Name of Only One Row
Example 3 explains how to switch only one row name instead of all row names of a data table. For this, we have to specify an index position behind the row.names function:
data3 <- data # Duplicate data frame row.names(data3)[2] <- 99 # Change row name of one row data3 # Print updated data frame # x1 x2 x3 # 1 1 a 8 # 99 2 b 8 # 3 3 c 8 # 4 4 d 8 # 5 5 e 8
Only the second row name was set to the numeric value 99.
Example 4: Modifying Empty Row Names of Matrix
So far, we have only changed the row names of data with the data.frame class. In this example, I’ll show how to modify row names of a matrix.
Note that row names of data frames and matrices can be renamed with more or less the same R syntax. However, for illustration I also want to show you an example that modifies the row names of a matrix.
First, we have to create an example matrix in R:
my_matrix <- matrix(1:12, nrow = 4) # Create example matrix my_matrix # Print example matrix # [,1] [,2] [,3] # [1,] 1 5 9 # [2,] 2 6 10 # [3,] 3 7 11 # [4,] 4 8 12
As you can see, our example matrix has four rows and three columns. The row and column names are empty.
If we want to change the empty row names of our matrix to a numeric range, we can use the row.names and the nrow functions as shown below:
row.names(my_matrix) <- 1:nrow(my_matrix) # Change row names of matrix my_matrix # Print updated matrix # [,1] [,2] [,3] # 1 1 5 9 # 2 2 6 10 # 3 3 7 11 # 4 4 8 12
The updated matrix has row names that are reflecting the row number of our matrix.
Video & Further Resources
Would you like to learn more about row names in R? Then you might have a look at the following video of my YouTube channel. In the video, I’m explaining the R programming codes of this tutorial.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to have a look at the other R programming tutorials of my homepage.
- How to Rename a Column Name in R
- Merge Data Frames by Row Names
- Convert Values in Column into Row Names of Data Frame
- Convert Row Names into Column of Data Frame in R
- R Programming Examples
To summarize: At this point you should know how to adjust row names of data frames and matrices in R programming. In case you have further comments or questions, let me know in the comments.
Statistics Globe Newsletter