Specify Row Names when Reading Excel File in R
Last Updated :
31 Aug, 2021
In this article, we are going to specify the row names when reading Excel file in the R Programming language.
Specifying row names when reading a file using row.names argument of the read.xlsx2() function.
This is the easiest approach to specify the row names when reading a file in the R programming language as in this approach the user needs to just call the read.xlsx() function which is particularly used for importing the excel type size, and used the row.names argument of this function with the required values into it and this will be reading to the specifying row names as per mentioned as the argument value with reading o file in the R programming language. Also, xlsx package, for this approach the user needs to install and import the xlsx package to the R working console as the function used here is from this package.
To install and import the xlsx package the user needs to follow the below syntax:
install.packages("xlsx")
library("xlsx")
Read.xlsx2() function: This function is used to read data from an Excel file or Workbook object into data.frame
Syntax: read.xlsx(xlsxFile, sheet, startRow = 1, colNames = TRUE, rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE, skipEmptyCols = TRUE, rows = NULL, cols = NULL,check.names = FALSE, sep.names = ".", namedRegion = NULL, na.strings = "NA", fillMergedCells = FALSE,row.names)
Excel file for demonstration
We will be using the xlsx file of 6 rows and 5 columns and with the use of the row.names argument we will be specifying the row names to 1 while reading this file.
R
library(xlsx)
gfg_file = read.xlsx2("GFG_DATA.xlsx",
sheetIndex = 1,
row.names = 1)
gfg_file
Output:

Specify Row Names when Reading a File
We will be using the xlsx file of 6 rows and 5 columns same as used in the previous example and with the use of the row.names argument we will specify the row names to 4 while reading this file.
R
library(xlsx)
gfg_file = read.xlsx2("GFG_DATA.xlsx",
sheetIndex = 1,
row.names = 4)
gfg_file
Output:
Similar Reads
Skip Rows While Using read_excel or read.excel in R When importing data from Excel files in R, you often encounter situations where certain rows are irrelevant to your analysis and should be skipped. This could be due to header rows, metadata, or other non-data rows. R provides several methods to handle this using packages like readxl and openxlsx. T
4 min read
Working with Excel Files in R Programming Excel files are of extension .xls, .xlsx and .csv(comma-separated values). To start working with excel files in R Programming Language, we need to first import excel files in RStudio or any other R supporting IDE(Integrated development environment). Reading Excel Files in R Programming Language Firs
3 min read
How to read multiple Excel files in R In this article, we will discuss how to merge multiple Excel files in the R programming language. Modules Used:dplyr: The dplyr package in R is a structure of data manipulation that provides a uniform set of verbs, helping to resolve the most frequent data manipulation hurdles.plyr: The âplyrâ packa
2 min read
How to read excel file in R ? We may work with structured data from spreadsheets, take advantage of R's capabilities for data analysis and manipulation, and incorporate Excel data into other R processes and packages by reading Excel files in R. The readxl package offers a simple and effective method for reading Excel files into
3 min read
Export CSV File without Row Names in R In this article, we will learn how to export CSV files without row names in R Programming Language. In R language we use write.csv() function to create a CSV file from the data. Syntax: write.csv(df, path) Parameters: df: dataframe objectpath: local path on your system where .csv file will be writ
1 min read
How to read Excel file and select specific rows and columns in R? In this article, we will discuss how to read an Excel file and select specific rows and columns from it using R Programming Language. File Used: To read an Excel file into R we have to pass its path as an argument to read_excel() function readxl library. Syntax: read_excel(path) To select a specific
2 min read