Lesson 12 of 17 · Week 06 · Thu Oct 29
Writing Data
Questions
- How do I save plots and data that I create in R?
Objectives
- Save a plot with
ggsave(), and several plots into one PDF withpdf()/dev.off(). - Write a data frame to CSV with
write.csv(), without row names. - Check output by reading it back.
Outputs belong in results/
Your analysis produces things: cleaned data, figures, tables. Save them from code, into results/, so they
can always be regenerated from data/ + scripts/. Never save over the raw data.
Adapted from Software Carpentry's “Writing Data” episode (CC BY 4.0, The Carpentries). Text, examples and exercises have been rewritten for this course.
Saving plots
library(ggplot2)
p <- ggplot(gm, aes(x = year, y = lifeExp, group = country)) + geom_line()
ggsave("results/lifeexp_lines.png", plot = p, width = 7, height = 4) # size in inches
ggsave() picks the format from the file extension (.png, .pdf, .svg).
To put several plots in one PDF, one per page, open a PDF device, print each plot, and close the
device with dev.off(). Until you close it, the file isn't finished.
pdf("results/lifeexp.pdf", width = 8, height = 5)
print(p) # page 1
print(p + facet_wrap(~ continent)) # page 2
dev.off()
Inside scripts and functions, ggplot objects need an explicit print() to be drawn on a device.
Writing data
recent <- gm[gm$year >= 2000, ]
write.csv(recent, file = "results/gm_recent.csv", row.names = FALSE)
row.names = FALSE stops R from adding an unnamed first column of row numbers. Other useful arguments:
quote = FALSE (no quotes around text; only safe if no value contains a comma) and na = "" (how to
write missing values). The readr package's write_csv() has sensible defaults and is faster for big
files.
Check the round trip
back <- read.csv("results/gm_recent.csv", stringsAsFactors = TRUE)
dim(back) == dim(recent)
all.equal(back$lifeExp, recent$lifeExp)
Reading back what you wrote is a cheap check that the file holds what you think it does.
A good habit is a separate cleaning script (e.g. scripts/01-clean.R) that reads raw data and
writes the cleaned version, and analysis scripts that start from the cleaned file.
Exercises
Each exercise gives you up to 10 checked attempts. Use Run as many times as you like to experiment first, then click Check answer when you're ready. Your exercises use numbers/subsets unique to you, so answers won't match a classmate's exactly.
Exercise 1 — A two-page PDF
0 / 10 attempts usedIn this exercise's pretend project, create results/gm_plots.pdf with two pages: page 1, a
scatterplot of life expectancy against GDP per capita (log x-axis); page 2, the same plot split into one panel per continent
with facet_grid() or facet_wrap(). Close the device when you're done.
Exercise 2 — A cleaning script's output
0 / 10 attempts usedWrite the cleaning step for a smaller dataset: keep only the rows of gm from years after
cut_year and from the continents in keep (click Run for yours). Store them in
gm_clean and write it to results/gm_clean.csv without row names. Then read it back
into back to check.
Key Points
- Save outputs from code, into
results/; never overwrite raw data. pdf()opens a multi-page file; eachprint()adds a page;dev.off()finishes it.write.csv(x, file, row.names = FALSE)writes a clean CSV.- Read back what you wrote to check it.
Proof of completion
Once you've attempted every exercise above, download a PDF summary of your work on this lesson — your name, the exercises you passed, attempt counts, timing, and your final code for each.