Open In App
Related Articles

Reading and Writing Excel Files With R Using readxl and writexl

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article let’s discuss reading and writing excel files using readxl and writexl packages of the R programming language.

read_excel() method in readxl Package:

The Readxl package is used to read data from the excel files i.e. the files of format .xls and .xlsx. The Readxl package provides a function called read_excel() which is used to read the data from excel files. The read_excel() method accepts the excel file which needs to read the content from it. In order to use the read_excel() method, first readxl library needs to be imported. 

Syntax

library(readxl)
data<-read_excel(‘excelFile’,sheet=1)

Parameters

  • excelFile – It specifies the excel file from which we read data.
  • sheet – It specifies from which work sheet we need to read. It is an optional parameter.

Note:- To install the Readxl package in R, use install.packages(“readxl”) command.

Syntax to install and import the readxl package:

install.package('readxl')
library(readxl)

Example: 

Here let’s look into how to read the data from an excel file using read_excel() method present in Readxl package.

Link to the used file:- link

R

# import readxl package
library(readxl)
  
# reading data from an excel file
data <-read_excel('stats.xlsx')  
print(data)

                    

Output:

 Name    Age Runs   Wickets
1 Akhil    21 143    14
2 Mriganka 32 1020   NA
3 Avinash  30 2022   2

write_xlsx() method in writexl package:

The writexl package provides a method called write_xlsx() method which allows writing a data frame into an excel sheet i.e. the files of format .xls and .xlsx. The write_xlsx() method accepts a data frame and the name of the excel file in which the content of the data frame is copied into it. In order to use the write_xlsx() method, the first writexl library needs to be imported.

Syntax

library(writexl)

write_xlsx(dataframeName, “excelFile”, col_names=TRUE)

Parameters

  • dataframeName – Name of the data frame that contains the data.
  • excelFile – Name of the excel file into which we import from data frame.
  • col_names – Write column names at the top of file if it set to True.

Syntax to install and import the writexl package:

install.package('writexl')
library(writexl)

Example: 

In the below code we created a data frame named “data” and imported the data in that data frame into the “write.xlsx” excel file using write_xlsx() method.

Link to the used file:- link

R

# import writexl library
library(writexl)
  
# create a data frameĀ 
data <- data.frame(player=c('A', 'B', 'C', 'D'),
                runs=c(100, 200, 408, NA),
                wickets=c(17, 20, NA, 5))
  
# import data in dataframe to an excel sheet
write_xlsx(data, "write.xlsx")

                    

Output

 



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