Open In App

Import multiple excel sheets into in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to import multiple Excel sheets into the R language. 

Excel provides us with multiple worksheets. For example, in the below Excel workbook StudentData, we have two worksheets – sheet 1 is Student Details and sheet 2 is Subject Details.

For importing multiple Excel sheets into R, we have to, first install a package in R which is known as readxl. After successfully installing the package, we have to load the package using the library function is R.

install.packages('readxl')

Once we have completely installed and loaded the package in RStudio, the next job is to import the excel workbook and check the number of sheet it contains. We can do this using the  excel_sheets function. 

R




library("read_excel")   
  
# Importing the excel workbook for
# checking the number of sheets it contains
excel_sheets("StudentData.xlsx")


Output:

'StudentDetails' 'SubjectDetails'

We have an Excel file named as “StudentData” and we have already saved it in our working directory. It contains two sheets named StudentDetails and SubjectDetails. We have a function in R called read_excel() which we will use to import specific sheet into R. If no argument is specified, then the read_excel() will by default import the first Excel sheet.

Syntax: read_excel(arg)

Code:

R




# Importing specific sheets into R using the read_excel()
StudentDet<-read_excel("StudentData.xlsx"
                       sheet = 1)
StudentDet<-read_excel("StudentData.xlsx"
                       sheet = "StudentDetails")
SubjectDet<-read_excel("StudentData.xlsx",
                       sheet = "SubjectDetails")
  
# For viewing the details of sheet 1
head(StudentDet)
# For viewing the details of sheet 2
head(SubjectDet)


Output:



Last Updated : 17 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads