Practical R code examples of examples of reading and writing CSV files

If you work with data in R, you live in CSV land. You import them, clean them, export them, and repeat. Having clear, practical examples of examples of reading and writing CSV files in R saves you from constantly searching old scripts or Stack Overflow threads. In this guide, I’ll walk through real examples that mirror what analysts, data scientists, and researchers actually do every day. We’ll start with a simple example of loading a CSV from disk, then move into more realistic workflows: reading large files efficiently, handling bad encoding, exporting clean analysis-ready tables, and writing CSVs directly to compressed formats. These examples include both base R and the `readr` package from the tidyverse, so you can choose whichever style fits your projects. Along the way, I’ll point out common pitfalls, performance tips for 2024-era datasets, and how to keep your CSV imports reproducible and readable for your future self (and your teammates).
Written by
Jamie
Published

Quick-start examples of reading and writing CSV files in R

Before getting lost in theory, let’s look at a few short, realistic code snippets. These are the best examples people copy-paste into real projects.

Simple example of reading a CSV with base R

## Basic read with base R
sales <- read.csv("data/monthly_sales.csv", stringsAsFactors = FALSE)

str(sales)
head(sales)

This is the classic pattern you see in many examples of reading and writing CSV files: a single call to read.csv(), turning off automatic factors, then a quick structure check with str() and head().

Simple example of writing a CSV with base R

## After some analysis...
summary_table <- aggregate(revenue ~ region, data = sales, FUN = sum)

## Write to CSV without row names
write.csv(summary_table, "output/region_revenue.csv", row.names = FALSE)

That tiny row.names = FALSE flag is one of the best examples of a small option that prevents annoying extra columns later.


Modern tidyverse examples of examples of reading and writing CSV files

Most R teams in 2024 lean heavily on the tidyverse for data work. Here are real examples of examples of reading and writing CSV files with readr, which is both faster and more explicit about column types.

Reading CSVs with readr::read_csv()

library(readr)

sales <- read_csv("data/monthly_sales.csv")

## Inspect parsing
spec(sales)

read_csv() prints a parsing summary and stores a column specification. That spec is one of the best examples of built-in documentation: it tells you exactly how each column was interpreted.

You can make the import reproducible by setting column types:

sales <- read_csv(
  "data/monthly_sales.csv",
  col_types = cols(
    date       = col_date(format = "%Y-%m-%d"),
    region     = col_factor(),
    product_id = col_character(),
    revenue    = col_double()
  )
)

In real examples from production code, explicitly setting col_types avoids subtle bugs when a column that used to be numeric suddenly gets a stray text value.

Writing CSVs with readr::write_csv()

library(dplyr)

summary_table <- sales %>%
  group_by(region) %>%
  summarize(
    total_revenue = sum(revenue, na.rm = TRUE),
    .groups = "drop"
  )

write_csv(summary_table, "output/region_revenue_tidy.csv")

write_csv() avoids row names and uses UTF‑8 by default, which makes it easier to share files across systems.


Real examples of reading large CSV files efficiently

In 2024, CSVs with millions of rows are common, not exotic. These examples of examples of reading and writing CSV files focus on performance and memory.

Example of reading only selected columns

Often, you only need a handful of columns from a huge file. Here’s how to avoid loading the entire thing into memory:

library(readr)

cols_to_keep <- c("date", "region", "revenue")

sales_small <- read_csv(
  "data/monthly_sales_big.csv",
  col_select = all_of(cols_to_keep)
)

col_select is one of the best examples of a small change that dramatically speeds up I/O and reduces RAM usage.

Example of reading in chunks (streaming)

For truly large files, chunked reading lets you process data in pieces:

library(readr)

callback <- function(x, pos) {
#  # x is a data frame for this chunk
  x |>
    group_by(region) |>
    summarize(revenue = sum(revenue, na.rm = TRUE), .groups = "drop")
}

chunked <- read_csv_chunked(
  "data/monthly_sales_big.csv",
  callback = SideEffectChunkCallback$new(callback),
  chunk_size = 100000
)

In real examples from analytics pipelines, chunked reading is paired with writing results to a database or a summary CSV instead of keeping everything in memory.


Handling messy, real‑world CSVs: examples include encoding, delimiters, and NA values

Most CSVs in the wild are not clean. These examples of examples of reading and writing CSV files show how to survive the messy ones.

Example of handling non‑UTF‑8 encoding

library(readr)

customers <- read_csv(
  "data/customers_latin1.csv",
  locale = locale(encoding = "Latin1")
)

This kind of pattern shows up in real examples from international teams that receive files exported from older Windows systems.

Example of custom NA strings and decimal marks

lab_results <- read_csv(
  "data/lab_results.csv",
  na = c("", "NA", "N/A", "-"),
  locale = locale(decimal_mark = ",")
)

If you work with health or clinical data, you’ll see similar patterns in data shared between institutions. For context on how CSVs fit into broader data sharing practices, the National Institutes of Health discusses data formats and sharing expectations in its data management resources at https://www.nih.gov.

Example of reading non‑standard delimiters

## Semicolon-delimited file
survey <- read_delim(
  "data/survey_semicolon.txt",
  delim = ";",
  escape_double = FALSE,
  trim_ws = TRUE
)

Real examples include CSVs exported from European spreadsheet software that defaults to semicolons when the comma is used as a decimal mark.


Clean export examples of examples of reading and writing CSV files

Writing CSVs is not just write.csv() and forget. Good exports are consistent, documented, and friendly to whoever imports them next.

Example of writing CSV with explicit NA handling

write_csv(
  lab_results,
  "output/lab_results_clean.csv",
  na = ""
)

Leaving empty strings for missing values is common when sharing with tools that don’t understand NA as a missing marker.

Example of writing compressed CSVs

With storage and network constraints, compressed CSVs are no longer exotic. R can write directly to .gz paths:

write_csv(
  sales,
  "output/monthly_sales_2024.csv.gz"
)

On modern datasets, this pattern is one of the best examples of a tiny change that can cut file size by 80–90% without changing any downstream R code that reads the file.

Example of writing CSV for cross‑platform sharing

When you share data with colleagues using Excel or other tools, a few small decisions matter:

write_csv(
  summary_table,
  "output/region_revenue_excel_friendly.csv",
  na = "",
  quote = "needed"
)

This avoids unnecessary quotes while still protecting text with commas. It’s a practical example of balancing human readability with machine parsing.


Reproducible pipelines: examples include reading raw CSVs, cleaning, and writing outputs

Most real examples of examples of reading and writing CSV files are part of a pipeline: read raw data, transform, write clean outputs. Here’s a compact but realistic pattern using dplyr.

library(readr)
library(dplyr)

raw <- read_csv(
  "data/visits_raw.csv",
  col_types = cols(
    patient_id = col_character(),
    visit_date = col_date(format = "%Y-%m-%d"),
    age        = col_double(),
    bmi        = col_double(),
    site       = col_character()
  )
)

clean <- raw %>%
  filter(!is.na(patient_id), !is.na(visit_date)) %>%
  mutate(
    age_group = case_when(
      age < 18 ~ "child",
      age < 65 ~ "adult",
      TRUE     ~ "older_adult"
    )
  )

write_csv(clean, "output/visits_clean.csv")

Public health and clinical research teams often follow patterns like this when preparing CSVs for analysis or sharing. For broader context on data cleaning and analysis workflows in health research, the Centers for Disease Control and Prevention provides guidance on public health data tools at https://www.cdc.gov, and the Mayo Clinic discusses how data supports clinical research at https://www.mayoclinic.org.


Despite the rise of Parquet, Feather, and databases, CSV remains everywhere: government open data portals, clinical registries, survey exports, and quick ad‑hoc data pulls. In 2024 and 2025, the best examples of production R workflows usually combine CSV with more modern storage:

  • Raw data lands as CSV from external partners.
  • R scripts read those CSVs, validate them, and write cleaned CSVs.
  • A subsequent step loads those cleaned files into databases or columnar formats for heavy analysis.

This is why having strong, realistic examples of examples of reading and writing CSV files in R still matters. Even if your final data lives in a database, CSV is often the front door to your pipeline.

If you work in research or education, universities such as Harvard provide open course materials that frequently use CSV in R tutorials; see data science resources at https://www.harvard.edu for context on teaching practices.


FAQ: short answers with concrete examples

What are some practical examples of reading CSV files in R?

Practical examples include:

  • Loading a local file with read_csv("data/file.csv") for quick analysis.
  • Reading only a few columns from a huge file using col_select.
  • Importing international data with locale(encoding = "Latin1") or a custom decimal mark.
  • Streaming a massive log file with read_csv_chunked() and summarizing each chunk.

These examples of reading and writing CSV files show up in analytics, research, and production ETL scripts.

Can you give an example of writing a clean CSV for others to use?

A simple, realistic example of a clean export looks like this:

write_csv(
  summary_table,
  "output/summary_for_collaborators.csv",
  na = "",
  quote = "needed"
)

This avoids row names, handles missing values in a friendly way, and keeps quoting under control.

What are the best examples of when CSV is still a good choice in 2024?

The best examples include:

  • Sharing small to medium tables with colleagues who live in Excel.
  • Publishing data alongside a research paper, where CSV is expected.
  • Interfacing with government or institutional open data portals that provide CSV downloads.
  • Logging outputs from scheduled R scripts where simplicity matters more than advanced features.

In each case, the examples of examples of reading and writing CSV files in this guide map directly to those scenarios.

How do I test my CSV reading code?

A practical approach is to create a tiny test CSV that mirrors the structure of your real data: include dates, numbers, text with commas, and missing values. Then run your import code against that file and verify types and values. Once that small example of a CSV works as expected, you can scale up to the full dataset with far more confidence.

Explore More R Code Snippets

Discover more examples and insights in this category.

View All R Code Snippets