R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Exporting data is a common task when working with R scripts. Depending on the format you want, there are multiple ways to export data from R to external files like CSV, Excel, or databases.
In this tutorial, we'll explore the basics of exporting data from R scripts to different file formats.
The write.csv()
function is an easy way to export data frames to CSV.
# Create a data frame data <- data.frame(Name = c("John", "Jane", "Doe"), Age = c(28, 34, 45), Gender = c("Male", "Female", "Male")) # Export to CSV write.csv(data, "data.csv", row.names = FALSE)
The writexl
package provides a simple way to write data to Excel.
install.packages("writexl") library(writexl) write_xlsx(data, "data.xlsx")
You can export data to a plain text file using write.table()
.
write.table(data, "data.txt", sep = "\t", row.names = FALSE)
The haven
package supports exporting to SPSS, Stata, or SAS formats.
install.packages("haven") library(haven) # To SPSS write_sav(data, "data.sav") # To Stata write_dta(data, "data.dta") # To SAS write_sas(data, "data.sas7bdat")
You can export data frames to databases using the DBI
and corresponding database-specific packages.
For SQLite (as an example):
install.packages("DBI") install.packages("RSQLite") library(DBI) library(RSQLite) # Create a connection con <- dbConnect(RSQLite::SQLite(), "database.sqlite") # Write data to database dbWriteTable(con, "table_name", data) # Close connection dbDisconnect(con)
For saving and loading objects in R's binary format:
# Save to RDS saveRDS(data, "data.rds") # To load back data2 <- readRDS("data.rds")
You can save multiple R objects to .RData
format.
var1 <- "hello" var2 <- 123 # Save multiple objects save(var1, var2, file = "data.RData") # To load back load("data.RData")
Using the jsonlite
package, you can export data to JSON format.
install.packages("jsonlite") library(jsonlite) write_json(data, "data.json")
Exporting data from R allows you to share your results, use them in other software or applications, or simply archive them for later use. Depending on the target software or the intended use-case, R provides various ways to export your data efficiently. Always ensure that the export format aligns with the requirements of your subsequent tasks or workflows.
Exporting data to CSV in R:
write.csv()
to export a data frame to a CSV file.# Create a sample data frame my_data <- data.frame( Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 22) ) # Export data frame to CSV write.csv(my_data, "my_data.csv", row.names = FALSE)
R saveRDS function example:
saveRDS()
function, which allows you to save a single R object to a file in a binary format, preserving its structure.# Save a data frame using saveRDS saveRDS(my_data, file = "my_data.rds")
Write.xlsx or writexl in R:
writexl
or WriteXLS
package to export data to Excel format.# Install and load the writexl package install.packages("writexl") library(writexl) # Export data frame to Excel write_xlsx(my_data, path = "my_data.xlsx")
Exporting data to text files in R:
write.table()
or writeLines()
, depending on the desired format.# Export data frame to a text file write.table(my_data, "my_data.txt", sep = "\t", row.names = FALSE)
Exporting data to Excel in R:
openxlsx
or writexl
.# Install and load the openxlsx package install.packages("openxlsx") library(openxlsx) # Export data frame to Excel write.xlsx(my_data, "my_data_excel.xlsx")
Save data frames as RDS files in R:
saveRDS()
function, specifically highlighting its use for saving and later loading entire data frames in a binary format.# Save and load a data frame using saveRDS and readRDS saveRDS(my_data, file = "my_data.rds") loaded_data <- readRDS("my_data.rds")