Exporting Data From R
In the previous chapters we described the essentials of R programming as well as how to import data into R. Here, you’ll learn how to export data from R to txt, csv, Excel (xls, xlsx) and R data file formats. Additionally, we’ll describe how to create and format Word and PowerPoint documents from R.
- Export Data From R to txt|csv|Excel files
- Create and format word and powerpoint documents using R and ReporteRs package:
- R base functions for writing data: write.table(), write.csv(), write.csv2()
- Writing data to a file
# Loading mtcars data
data("mtcars")
# Write data to txt file: tab separated values
# sep = "\t"
write.table(mtcars, file = "mtcars.txt", sep = "\t",
row.names = TRUE, col.names = NA)
# Write data to csv files:
# decimal point = "." and value separators = comma (",")
write.csv(mtcars, file = "mtcars.csv")
# Write data to csv files:
# decimal point = comma (",") and value separators = semicolon (";")
write.csv2(mtcars, file = "mtcars.csv")
Read more: Writing data from R to a txt|csv file: R base functions
- Installing and loading readr: install.packages(“readr”)
- readr functions for writing data: write_tsv(), write_csv()
- Writing data to a file
# Loading mtcars data
data("mtcars")
library("readr")
# Writing mtcars data to a tsv file
write_tsv(mtcars, path = "mtcars.txt")
# Writing mtcars data to a csv file
write_csv(mtcars, path = "mtcars.csv")
Read more: Fast writing of Data From R to txt|csv Files: readr package
- Installing xlsx package: install.packages(“xlsx”)
- Using xlsx package: write.xlsx()
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)
Read more: Writing data from R to Excel files (xls|xlsx)
- Save one object to a file: saveRDS(object, file), readRDS(file)
- Save multiple objects to a file: save(data1, data2, file), load(file)
- Save your entire workspace: save.image(), load()
- Saving and restoring one single R object:
# Save a single object to a file
saveRDS(mtcars, "mtcars.rds")
# Restore it under a different name
my_data <- readRDS("mtcars.rds")
- Saving and restoring one or more R objects:
# Save multiple objects
save(data1, data2, file = "data.RData")
# To load the data again
load("data.RData")
- Saving and restoring your entire workspace:
# Save your workspace
save.image(file = "my_work_space.RData")
# Load the workspace again
load("my_work_space.RData")
Read more: Saving data into R data format: RDATA and RDS
ReporteRs package, by David Gohel, provides easy to use functions to write and format Word documents. It can be also used to generate Word document from a template file with logos, fonts, etc. ReporteRs is Java-based solution, so it works on Windows, Linux and Mac OS systems.
- Install and load the ReporteRs R package
- Create a simple Word document
- Add texts : title and paragraphs of texts
- Format the text of a Word document using R software
- Add plots and images
- Add a table
- Add lists : ordered and unordered lists
- Add a footnote to a Word document
- Add R scripts
- Add a table of contents into a Word document
Read more: Create and format Word documents with R and ReporteRs package
- Quick introduction to ReporteRs package
- Create a Word document using a template file
Read more: Create a Word document from a template file with R and ReporteRs package
- Add a simple table
- Add a formatted table
- Change the background colors of rows and columns
- Change cell background and text colors
- Insert content into a table : header and footer rows
- Analyze, format and export a correlation matrix into a Word document
- Powerpoint
Read more: Add a table into a Word document with R and ReporteRs package
- Why is it important to be able to generate a PowerPoint report from R ?
- Reason I : Many collaborators works with Microsoft office tools
- Reason II : keeping beautiful R graphs beautiful for publications
- Install and load the ReporteRs package
- Create a simple PowerPoint document
- Slide layout
- Generate a simple PowerPoint document from R software
- Format the text of a PowerPoint document
- Add plots and images
- Add a table
- Add ordered and unordered lists
- Create a PowerPoint document from a template file
Read more: Create and format PowerPoint documents with R and ReporteRs
- Case of base graphs
- Case of graphs generated using ggplot2
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)
Articles contained by this category :
Add a table into a Word document using R software and ReporteRs package
Create a Word document from a template file using R software and ReporteRs package
Create an editable graph from R software
Create and format PowerPoint documents from R software
Create and format Word documents using R software and Reporters package
Fast Writing of Data From R to txt|csv Files: readr package
R xlsx package : A quick start guide to manipulate Excel files in R
r2excel: Read, write and format easily Excel files using R software
Saving Data into R Data Format: RDS and RDATA
Writing Data From R to Excel Files (xls|xlsx)
Writing Data From R to txt|csv Files: R Base Functions