Importing and Exporting Data

Working with data in R requires importing datasets from various sources and exporting processed data for reporting or sharing. R provides multiple functions and packages to handle different file formats efficiently.

1. Importing Data

a) Reading CSV Files

# Base R method
data <- read.csv("data.csv", header = TRUE, stringsAsFactors = FALSE)# Preview data
head(data)

Key Parameters:

  • header = TRUE: First row contains column names
  • stringsAsFactors = FALSE: Keeps text as character, not factor

b) Reading Excel Files

Requires readxl package:

library(readxl)# Read Excel file
data <- read_excel("data.xlsx", sheet = 1)# Preview data
head(data)

c) Reading Text Files

data <- read.table("data.txt", header = TRUE, sep = "\t")  # Tab-separated

d) Importing Data from Web

url <- "https://example.com/data.csv"
data <- read.csv(url, header = TRUE)

e) Using readr for Faster Imports

readr is part of tidyverse for efficient data reading:

library(readr)
data <- read_csv("data.csv")

2. Exporting Data

a) Writing CSV Files

write.csv(data, "output.csv", row.names = FALSE)
  • row.names = FALSE prevents R from adding row numbers

b) Writing Excel Files

Requires writexl package:

library(writexl)
write_xlsx(data, "output.xlsx")

c) Writing Text Files

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

d) Exporting for RDS Format

RDS files preserve R objects and structure:

saveRDS(data, "data.rds")       # Save
data2 <- readRDS("data.rds") # Load back

3. Tips for Efficient Data Import/Export

  • Always check column types after importing (str(data))
  • Handle missing values during import with na.strings parameter
  • Use relative paths or full file paths to avoid file-not-found errors
  • For large datasets, prefer data.table::fread() for fast reading

4. Advantages

  • Easily work with external datasets in R
  • Share processed data with others in common formats
  • Supports multiple file types: CSV, Excel, text, RDS, databases
  • Streamlines data cleaning, analysis, and reporting workflow

Conclusion

Importing and exporting data in R is a fundamental step in the data analysis workflow. By mastering functions like read.csv(), read_excel(), write.csv(), and write_xlsx(), you can efficiently manage datasets, move data between R and other tools, and ensure smooth data processing for analysis and reporting.

Home ยป R Programming (R Lang) > R for Data Analysis > Importing and Exporting Data