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 from scripts in 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.

1. Exporting to CSV:

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)

2. Exporting to Excel:

The writexl package provides a simple way to write data to Excel.

install.packages("writexl")
library(writexl)

write_xlsx(data, "data.xlsx")

3. Exporting to Text:

You can export data to a plain text file using write.table().

write.table(data, "data.txt", sep = "\t", row.names = FALSE)

4. Exporting to SPSS, Stata, or SAS:

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")

5. Exporting to Databases:

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)

6. Exporting to RDS (R Binary Format):

For saving and loading objects in R's binary format:

# Save to RDS
saveRDS(data, "data.rds")

# To load back
data2 <- readRDS("data.rds")

7. Exporting to RData:

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")

8. Exporting to JSON:

Using the jsonlite package, you can export data to JSON format.

install.packages("jsonlite")
library(jsonlite)

write_json(data, "data.json")

Conclusion:

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.

  1. Exporting data to CSV in R:

    • Description: The CSV (Comma-Separated Values) format is widely used for exporting data. Show how to use write.csv() to export a data frame to a CSV file.
    • Code:
      # 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)
      
  2. R saveRDS function example:

    • Description: Introduce the saveRDS() function, which allows you to save a single R object to a file in a binary format, preserving its structure.
    • Code:
      # Save a data frame using saveRDS
      saveRDS(my_data, file = "my_data.rds")
      
  3. Write.xlsx or writexl in R:

    • Description: Show how to use the writexl or WriteXLS package to export data to Excel format.
    • Code:
      # Install and load the writexl package
      install.packages("writexl")
      library(writexl)
      
      # Export data frame to Excel
      write_xlsx(my_data, path = "my_data.xlsx")
      
  4. Exporting data to text files in R:

    • Description: Illustrate exporting data to text files using functions like write.table() or writeLines(), depending on the desired format.
    • Code:
      # Export data frame to a text file
      write.table(my_data, "my_data.txt", sep = "\t", row.names = FALSE)
      
  5. Exporting data to Excel in R:

    • Description: Demonstrate exporting data to Excel using additional packages like openxlsx or writexl.
    • Code:
      # Install and load the openxlsx package
      install.packages("openxlsx")
      library(openxlsx)
      
      # Export data frame to Excel
      write.xlsx(my_data, "my_data_excel.xlsx")
      
  6. Save data frames as RDS files in R:

    • Description: Revisit the saveRDS() function, specifically highlighting its use for saving and later loading entire data frames in a binary format.
    • Code:
      # Save and load a data frame using saveRDS and readRDS
      saveRDS(my_data, file = "my_data.rds")
      loaded_data <- readRDS("my_data.rds")