Lesson 12 of 17 · Week 06 · Thu Oct 29

Writing Data

Questions

Objectives

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 used

In 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.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Exercise 2 — A cleaning script's output

0 / 10 attempts used

Write 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.

Pasting isn't accepted here — please type your answer so it reflects your own work.

      

Key Points

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.