Specify Row Names when Reading a File in R (Example)


In this tutorial, I’ll illustrate how to manually set a specific column of imported data as row names in the R programming language.

The content of the page looks as follows:

Let’s dive right into the example!


Example Data & Packages

We’ll use the following Excel file as basement for this R tutorial:


excel file with row names


If we want to read these Excel data, we have to install and load the xlsx package:

install.packages("xlsx")                                    # Install xlsx package
library("xlsx")                                             # Load xlsx package

Now, we can apply the read.xlsx2 function to import our data:

data1 <- read.xlsx2("C:/Users/Joach/Desktop/my_file.xlsx",  # Default row names
                    sheetIndex = 1)
data1                                                       # Print imported data
#   row_names x1 x2 x3
# 1         a  5  3  2
# 2         b  4  7  3
# 3         c  2  5  9
# 4         d  7  6  1
# 5         e  1  7  2

As you can see based on the previous output of the RStudio console, our imported data has four columns and the first variable is called row_names.

Let’s assume that we want to import these data again, but the first variable should be set as actual row names of our data frame.

I’ll show you next how to do this!


Example: Specifying First Column as Row Names when Importing Data

In this example, I’ll illustrate how to set the first column of our Excel file as row names when we are reading the data.

For this, we have to specify the row.names argument within the read.xlsx2 function to be equal to 1:

data2 <- read.xlsx2("C:/Users/Joach/Desktop/my_file.xlsx",  # Column 1 as row names
                    sheetIndex = 1,
                    row.names = 1)
data2                                                       # Print imported data
#   x1 x2 x3
# a  5  3  2
# b  4  7  3
# c  2  5  9
# d  7  6  1
# e  1  7  2

Have a look at the previous output: The row names of our data frame were set to be equal to the first column of our Excel file.


Note that the read.xlsx2 is not the only function that is providing the row.names argument. The read.table function provides this argument as well, for example when we want to read CSV or txt files.


Video & Further Resources

Do you need more explanations on the R programming codes of this article? Then you may want to have a look at the following video of the Statistics Globe YouTube channel. I show the R programming codes of this tutorial in the video.


The YouTube video will be added soon.


In addition, you may read the related posts of this homepage. You can find a selection of articles about topics such as importing and exporting data below:


To summarize: In this tutorial you learned how to specify the row names of a data set during import in R. If you have further questions, don’t hesitate to let me know in the comments below.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.