Writing Data From R to Excel Files (xls|xlsx)
Previously, we described the essentials of R programming and provided quick start guides for reading and writing txt and csv files using R base functions as well as using a most modern R package named readr, which is faster (X10) than R base functions. We also described different ways for reading data from Excel files into R.
Preleminary tasks
Launch RStudio as described here: Running RStudio and setting up your working directory
Writing Excel files using xlsx package
The xlsx package, a java-based solution, is one of the powerful R packages to read, write and format Excel files.
Installing and loading xlsx package
- Install
install.packages("xlsx")
- Load
library("xlsx")
Using xlsx package
There are two main functions in xlsx package for writing both xls and xlsx Excel files: write.xlsx() and write.xlsx2() [faster on big files compared to write.xlsx function].
The simplified formats are:
write.xlsx(x, file, sheetName = "Sheet1",
col.names = TRUE, row.names = TRUE, append = FALSE)
write.xlsx2(x, file, sheetName = "Sheet1",
col.names = TRUE, row.names = TRUE, append = FALSE)
- x: a data.frame to be written into the workbook
- file: the path to the output file
- sheetName: a character string to use for the sheet name.
- col.names, row.names: a logical value specifying whether the column names/row names of x are to be written to the file
- append: a logical value indicating if x should be appended to an existing file.
Example of usage: the following R code will write the R built-in data sets - USArrests, mtcars and iris - into the same Excel file:
library("xlsx")
# Write the first data set in a new workbook
write.xlsx(USArrests, file = "myworkbook.xlsx",
sheetName = "USA-ARRESTS", append = FALSE)
# Add a second data set in a new worksheet
write.xlsx(mtcars, file = "myworkbook.xlsx",
sheetName="MTCARS", append=TRUE)
# Add a third data set
write.xlsx(iris, file = "myworkbook.xlsx",
sheetName="IRIS", append=TRUE)
Read more
Read more about for reading, writing and formatting Excel files:
Summary
Infos
This analysis has been performed using R (ver. 3.2.3).
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for you
This section contains the best data science and self-development resources to help you on your path.
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Click to follow us on Facebook :
Comment this article by clicking on "Discussion" button (top-right position of this page)