Open In App

How to Convert Numeric Dataframe to Text in xlsx File in R

Last Updated : 05 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to convert a numeric data frame which means the data present in both rows and columns of the data frame is of numeric type to an Excel file(.xlsx). To achieve this mechanism in R Programming, we have a package called writexl which contains the write_xlsx() function which is used to convert the data frame to an Excel File.

Syntax : write_xslx(dataframe,”path”,col_names,format_header,use_zip64)

where,

  • dataframe – The data frame name we want to convert to an excel file
  • path – path to store the created excel file
  • col_names – to ensure that column names are written at top of the file if assigned to TRUE
  • format_header – used to align the column names centric and bold if assigned to TRUE
  • use_zip64 – used to enable zip support for larger files(Greater than 4GB)

The code to convert numeric data frame to text in an xlsx file is given below.

R




# install required packages
install.packages("writexl")
 
# load the install packages into
# current working directory
library(writexl)
 
# creation of numeric data frame
data_frame <- data.frame("Roll_Number" <- c(1,2,3,4,5,6,7,8,9,10),
                        "Age"<- c(18,19,20,19,18,17,18,19,17,16),
                         "Marks" <- c(98,96,89,90,75,35,88,92,78,94))
 
# Converting using write_xlsx() function
writexl::write_xlsx(data_frame,
                    "Documents\\Sample.xlsx",
                    col_names=TRUE,
                    format_headers=TRUE,
                    use_zip64=FALSE)


Output:

Output

 

Explanation 

  1. First we installed the required packages which are used to convert the numeric data frame to excel file and loaded that package functions.
  2. Later using data.frame() function we created as sample numeric data frame of student details like roll number, age and marks.
  3. Using write.xlsx() function of writexl package we converted numeric data frame to Excel File at specified path.

Note

  1. During package installation it asks to select the CRAN mirror then on that window select your country.
  2. If we have not specified path in write.xlsx() function then by default the excel file is going to save where the R documents(Script files) are stored y default. 

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads