Skip to course content
Free R data course

R Foundations for Data Analysis

Unit 01.02: Four folders, and one rule about the first

A project is not a setting you switch on. It is a folder whose shape tells a stranger where everything is.

Four folders, and one rule about the first

The layout barely varies between analysts: raw data in one place, cleaned data in another, code in a third, outputs in a fourth. The names are less important than the separation.

One rule carries most of the value: nothing your code writes ever goes into the raw folder. Raw data is what arrived. If a script can overwrite it, then re-running the analysis can change its own inputs, and the second run is no longer the same experiment as the first. Keeping raw read-only makes 'delete everything and run it again' a safe move rather than a frightening one.

Editors add a project file on top of this, which mostly serves to set the working directory for you. Useful, but the folder discipline is what does the work.

This block builds the skeleton in a temporary directory and lists what ends up in it.


# A project is a folder with a predictable shape, not a pile of files.
project <- file.path(tempdir(), "ward-report")
unlink(project, recursive = TRUE)

folders <- c("data-raw", "data-clean", "R", "outputs")
for (f in folders) dir.create(file.path(project, f), recursive = TRUE)

# data-raw is never written to by your code. That is the whole point of it.
writeLines("ward,visits\nNorth,412\nSouth,388",
           file.path(project, "data-raw", "visits.csv"))
writeLines("# 01-clean.R\n# reads data-raw/, writes data-clean/",
           file.path(project, "R", "01-clean.R"))

found <- list.files(project, recursive = TRUE)
cat("Files under the project root:\n")
cat(paste0("  ", found), sep = "\n")
cat("\nRaw folder is read-only by convention, so re-running is always safe.\n")

Two files appear: data-raw/visits.csv and R/01-clean.R. The empty data-clean and outputs folders exist and stay empty until the script fills them, which is exactly the state you want at the start of a project — every folder that will hold generated files is visibly a folder that the code owns.

The mistake this prevents

The mistake is one folder of forty files where visits.csv, visits_v2.csv, visits_final.csv and visits_final_USE_THIS.csv sit beside the scripts. Nobody can tell which file the analysis actually read, including you, three weeks later.

Takeaway

Make the four folders before you write any code. Treat data-raw as read-only, and let everything under data-clean and outputs be disposable, because the script rebuilds them.