Orientation: The R Ecosystem & the Data Workflow
School of Economics, Quaid-i-Azam University · d4d
2026-08-30
This session is a map, not a marathon. You will not write production code today — you’ll learn what each piece is for, so later sessions make sense.
| Session | Focus | Mode |
|---|---|---|
| 1 (now) | The R ecosystem, project workflow, packages, data landscape | Orientation — mostly watching |
| 2 | Importing real data (csv, Excel, Stata, SPSS) | Hands-on |
| 3 | Cleaning, recoding, joining | Hands-on |
| 4 | Mini exercise with your own / provided dataset | Guided practice |
Note
Nothing here is timed to the minute — the facilitator will pace it live.
mean()You will practice all of this starting Session 2.
R is a free, open-source language for statistics and data analysis — not a single piece of software you double-click.
Think of it in layers:
Common confusion
“RStudio” and “R” are not the same thing. R does the work; RStudio is just the window you use to talk to it. You could use R without RStudio — but not the other way around.
Note
Both are free, both are made by Posit (formerly “RStudio, Inc.”). Your choice affects the window you look at, not the R code you write.
Rule of thumb
If an AI suggestion runs without error, that does not mean it’s correct. Always check the output against what you expect — the same discipline you’d apply to Stata do-files or SPSS syntax.
We’ll return to this briefly whenever it’s useful, not as a separate deep-dive.
Why it matters for you:
d4d training materials already live at github.com/Zahedasghar/d4dDeep dive later
Cloning, committing, pushing, pull requests — that’s a full session on Day 2/3. Today: just recognize the name and know it’s where the workshop repo lives.
A Project (.Rproj) anchors your work to one folder, so:
my-workshop-project/
├── my-workshop-project.Rproj
├── data/ # raw data — never edited by hand
│ └── cpi_2024.csv
├── R/ # scripts
│ └── 01_import.R
├── output/ # figures, cleaned data, tables
└── report.qmd # the write-up
Note
This is the same skeleton your d4d repository already follows — you’ll see it again in every workshop dataset this week.
R by itself does the basics. A package is a bundle of extra functions someone has written and shared — free, via CRAN.
Easy to mix up
install.packages() downloads the toolkit. library() opens the toolbox for this session. Forgetting library() is the #1 reason “it worked yesterday” stops working today.
| Package | What it’s for |
|---|---|
tidyverse |
Umbrella: dplyr, ggplot2, readr, tidyr, purrr and more |
janitor |
Cleaning messy column names, quick counts |
haven |
Reading Stata (.dta) and SPSS (.sav) files |
readxl |
Reading Excel (.xlsx) files |
jsonlite |
Reading JSON data |
httr2 |
Talking to web APIs |
here |
Reliable file paths inside a Project |
tidyverse is a meta-package — one library(tidyverse) loads about eight packages at once.
| Format | Package :: Function | You’ll see it in |
|---|---|---|
.csv |
readr::read_csv() |
PBS, WFP, WDI exports |
.xlsx |
readxl::read_excel() |
PBS CPI, HDR tables |
.dta (Stata) |
haven::read_dta() |
PDHS microdata |
.sav (SPSS) |
haven::read_sav() |
Some survey datasets |
.json |
jsonlite::fromJSON() |
HDX / API responses |
| Web API | httr2::request() … |
WDI, HDX CKAN API |
Same idea every time: pick the function that matches the file, point it at the path, get back a data frame.
janitor::clean_names() immediately after import is a habit worth building now — it turns "GDP (current US$)" into gdp_current_us.
haven Handles BothNote
If you’re coming from Stata or SPSS, this is often the most reassuring slide of the day — your existing datasets open in R without conversion.
library(jsonlite)
library(httr2)
# A file already saved as JSON
raw <- fromJSON(here("data", "wfp_prices.json"))
# Pulling data live from a web API (e.g. World Bank WDI)
resp <- request("https://api.worldbank.org/v2/country/pk/indicator/NY.GDP.MKTP.CD") |>
req_url_query(format = "json") |>
req_perform()
wdi_data <- resp_body_json(resp)Not today’s task
Web APIs bring in live data instead of a saved file — powerful, but with more moving parts (URLs, keys, rate limits). We’ll practice this with real Pakistan-relevant APIs (WDI, HDX) in a later session.
Before anything else, always check:
names(data) — are the column names sane?glimpse(data) — what type is each column?anyNA(data) — are there missing values you need to know about?Note
This “audit before proceeding” habit — printing what you found before trusting it — will come back throughout the week.
dplyr| Verb | Does |
|---|---|
select() |
Choose columns |
filter() |
Choose rows |
mutate() |
Create/change a column |
arrange() |
Sort rows |
summarise() |
Collapse to summary stats |
Nearly everything you’ll do to a dataset this week is some combination of these five.
Raw data often arrives as codes (1, 2, 3) instead of labels.
case_when() is the more general cousin — for conditions rather than exact matches (e.g. turning a continuous age variable into age groups).
When information about the same units (districts, households, years) lives in two separate tables, a join brings them together.
| Join type | Keeps |
|---|---|
left_join() |
Everything in the left table, matched where possible |
inner_join() |
Only rows that match in both tables |
anti_join() |
Rows in the left table with no match — great for auditing |
A lesson from real PBS/WFP data
Never assume a shared code column means the same thing in both files — always verify with anti_join() before trusting a join. Mismatched region codes or renamed categories cause silent data loss.
here() keep file paths from breakinginstall.packages() once, library() every sessionWe open RStudio, create a real Project, and import an actual PBS or WFP dataset together — csv first, then Excel.
Before we move on
Any names, terms, or steps from this session you’d like re-explained before we start typing?
Questions?
d4d · Day 1, Session 1 — Orientation