How to Convert Numeric Dataframe to Text in xlsx File in R
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:
Explanation
- First we installed the required packages which are used to convert the numeric data frame to excel file and loaded that package functions.
- Later using data.frame() function we created as sample numeric data frame of student details like roll number, age and marks.
- Using write.xlsx() function of writexl package we converted numeric data frame to Excel File at specified path.
Note
- During package installation it asks to select the CRAN mirror then on that window select your country.
- 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.