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 R as data frames for additional processing and analysis.

In this article, we will be discussing two different techniques to read or import an Excel file in R.

Approach

  • Import module
  • Pass the path of the file to the required function
  • Read file
  • Display content

Method 1: Using read_excel() from readxl

read_excel() function is basically used to import/read an Excel file and it can only be accessed after importing the readxl library in R language..

Syntax:

read_excel(path)

The read_excel() method extracts the data from the Excel file and returns it as an R data frame. It instantly recognizes the Excel file’s sheets and imports the data from the default sheet, which is often the first page. The sheet option allows us to give a specific sheet’s name or index in order to read that particular sheet.

Example:

R




library(readxl)
 
Data_gfg <- read_excel("Data_gfg.xlsx")
 
Data_gfg


Output:

     group    value1    value2
1  Group A  9.539645 11.584165
2  Group A 13.117417  9.469207
3  Group A 10.141017 16.337912
4  Group A 10.258575 14.415924
5  Group A 13.430130  9.753783
6  Group A 10.921832 11.194230
7  Group A  7.469878 11.066689
8  Group A  8.626294 13.559930
9  Group A  9.108676 11.833262
10 Group A 12.448164 12.506637

Method 2: Using read.xlsx() from xlsx

read.xlsx() function is imported from the xlsx library of R language and used to read/import an excel file in R language.

Syntax:

read.xlsx(path)

We can deal with structured data from spreadsheets and incorporate Excel data with other R packages and workflows by reading Excel files in R using the xlsx package. For additional processing and analysis, Excel files can be read into R as data frames using the read.xlsx() function.

Example:

R




install.packages("xlsx")
 
Data_gfg <-read.xlsx('Data_gfg.xlsx')
 
Data_gfg


Output:

     group    value1    value2
1  Group A  9.539645 11.584165
2  Group A 13.117417  9.469207
3  Group A 10.141017 16.337912
4  Group A 10.258575 14.415924
5  Group A 13.430130  9.753783
6  Group A 10.921832 11.194230
7  Group A  7.469878 11.066689
8  Group A  8.626294 13.559930
9  Group A  9.108676 11.833262
10 Group A 12.448164 12.506637

FAQ:

Q.1: Can we read multiple Excel files in one go?

Yes, we can read multiple Excel files by providing a vector of file paths to the read_excel() 
function. 

Q.2: How we can read a specific range of cells from an Excel file?

The read_excel() function allows us to specify the range of cells to be read using the range
parameter.

Q.3: Can we read Excel files from a specific worksheet by name instead of an index?

Yes, you can read Excel files from a specific worksheet by name using the sheetName parameter in 
the read_excel() function. 


Contact Us