How to use write.csv in R
Introduction
The write.csv() function in R allows you to export data frames to CSV (Comma-Separated Values) files, making your data accessible to other programs like Excel or databases. This function is essential when you need to save processed data, share results with colleagues, or create backups of your work.
Getting Started
library(tidyverse)
library(palmerpenguins)Example 1: Basic Usage
The Problem
You have a data frame in R and need to save it as a CSV file to your local directory. This is the most fundamental task when exporting data for external use or storage.
Step 1: Prepare sample data
We’ll start with a simple subset of the penguins dataset to demonstrate basic export functionality.
# Create a small dataset to export
penguin_sample <- penguins |>
filter(species == "Adelie") |>
select(species, island, bill_length_mm, body_mass_g) |>
slice_head(n = 10)This creates a manageable 10-row dataset with key penguin characteristics that we can easily export and verify.
Step 2: Export with basic write.csv
Now we’ll export our data using the most basic write.csv() syntax.
# Export to CSV file
write.csv(penguin_sample, "basic_penguins.csv")
# Verify the file was created
file.exists("basic_penguins.csv")The function creates a CSV file in your current working directory and returns TRUE when we check if the file exists.
Step 3: Control row names behavior
By default, write.csv() includes row names, but we usually don’t want them in our output file.
# Export without row names (more common)
write.csv(penguin_sample,
"penguins_no_rownames.csv",
row.names = FALSE)This creates a cleaner CSV file without the unnecessary row number column that R adds by default.
Example 2: Practical Application
The Problem
You’re analyzing penguin data and need to create separate CSV files for each species, customize the output format, and ensure the files can be easily imported by colleagues using different software. This represents a real-world data sharing scenario.
Step 1: Create processed datasets
First, we’ll create summary statistics for each penguin species that we want to share.
# Create species summaries
species_summaries <- penguins |>
group_by(species, island) |>
summarise(
avg_bill_length = round(mean(bill_length_mm, na.rm = TRUE), 2),
avg_body_mass = round(mean(body_mass_g, na.rm = TRUE), 0),
count = n(),
.groups = "drop"
)This creates a summary dataset with average measurements and counts for each species-island combination.
Step 2: Export with custom formatting
Now we’ll export this data with specific formatting options for better compatibility.
# Export with custom options for better compatibility
write.csv(species_summaries,
"penguin_species_summary.csv",
row.names = FALSE,
na = "")The na = "" parameter ensures that any missing values appear as empty cells rather than “NA” text, which is cleaner for most applications.
Step 3: Create multiple files programmatically
Finally, we’ll create separate CSV files for each species using a loop approach.
# Split data by species and export each
species_list <- split(penguins, penguins$species)
for (species_name in names(species_list)) {
filename <- paste0("penguins_", tolower(species_name), ".csv")
write.csv(species_list[[species_name]],
filename,
row.names = FALSE)
}This creates three separate CSV files (one for each penguin species) with descriptive filenames, making it easy for collaborators to work with specific subsets of the data.
Summary
- Use
write.csv(data, "filename.csv")for basic CSV export functionality - Always include
row.names = FALSEunless you specifically need row identifiers in your output - Use
na = ""to export missing values as empty cells instead of “NA” text for better compatibility - Combine
write.csv()with data manipulation functions to create multiple customized export files Remember that CSV files are saved to your current working directory unless you specify a full file path