Communicating Better With Data: Import, Export & APIs in R
Pakistan Social Datasets — PSLM, ASER, LFS, PDHS, MICS
1. Introduction
Data communication begins long before charts or dashboards. It starts from one fundamental skill: getting data in and out of your analytical environment — cleanly, reproducibly, and transparently.
In Pakistan’s context, major datasets appear in various formats:
Dataset
Institution
File Format
Notes
PSLM
Pakistan Bureau of Statistics
Excel, CSV
Often merged from district-level files
ASER
ASER Pakistan
Excel, CSV
Education indicators
LFS
PBS
Stata (.dta), CSV
Microdata often obtained via request
MICS
UNICEF
Stata (.dta)
Standardized sampling
PDHS
NIPS & DHS
Stata (.dta)
Includes household & individual files
This chapter teaches you how to work with these formats efficiently in R, with Python and Stata guidance provided in sidebars.
✔ Importing: CSV, XLSX, DTA, JSON ✔ Exporting to: CSV, XLSX, JSON, DTA ✔ APIs & Web Data ✔ Pakistan datasets (PSLM, ASER, LFS, PDHS, MICS) ✔ Cleaning & transforming data ✔ Visual communication with ggplot2 ✔ Reproducible workflows in Positron
10. Exercises (Pakistan-Data Focus)
Exercise 1: Import & Clean PSLM
Import PSLM 2020 CSV file
Clean district and province names
Create literacy summary table
Exercise 2: Import PDHS Individual File
Load .dta file
Create indicator for modern contraceptive use
Create summary by province
Exercise 3: Fetch JSON from a URL
Use JSON API
Convert to data frame
Plot a bar chart
If you’d like, I can also provide:
✅ A PDF Quarto version ✅ A full book chapter + exercises ✅ Additional chapters (Exploratory Data Analysis, Mapping Pakistan, Dashboarding with Quarto) ✓ Or convert this into Slides (Revealjs)
Would you like the next chapter: “Exploratory Data Analysis with Pakistani Social Datasets”?