Main reference for the course
Wickham, H., Cetinkaya-Rundel, M., Grolemund, G. 2023. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. O’Reilly Media, Incorporated.
Session 1 — Data Management
The slides were adapted from those of Pierre Michel and Morgan Raux, both researchers at AMSE, who kindly shared their work.
This slide deck was made with Quarto and reveal.js, it is translated by Claude Sonnet 5 from a LaTex presentation previously made with beamer.
R languageR + tips to learn autonomouslyWarning This course does not cover all regression techniques!
.R scriptsWickham, H., Cetinkaya-Rundel, M., Grolemund, G. 2023. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. O’Reilly Media, Incorporated.
This course will (hopefully) be useful for:
This course will particularly be useful for those who want to work as economist, analyst, data scientist or consultant. Nowadays, a lot goes through data analysis.
Questions?
R (1995, AT&T Bell Laboratories) is a software for statistical analysis and graphics, it is a clone of S-PLUS, mainly written in C language.R is free software, distributed freely under the terms of GNU Public Licence of the Free Software Foundation (FSF). The development and distribution are ensured by several statisticians (R Development Core Team). It is compatible with all platforms.R, tutorials and updates are available from the CRAN (Comprehensive R Archive Network).R:R on your computerR is a programming language, it consists of a series of tools (packages)R, your computer can load these packages and let you use their content (functions, datasets, help pages)R has been installed, download RStudio:RThe R programming language is:
R evaluates whatever you type and prints the result right away, exactly like a calculator.Enter.<-.x <- 5 as “x gets the value 5”.Tip = also works as an assignment operator in R, but <- is the convention used by the R community: stick to it!
Every piece of data in R has a type. The three most common ones you will encounter are:
2, 3.14, -7)."Marseille", "A").TRUE, FALSE or NA_logical.class() tells you the type of any object in R.Tip Logical values behave like numbers in disguise: TRUE equals 1 and FALSE equals 0. Try TRUE + TRUE in your console!
c() (for “combine”).ages <- c(25, 31, 42, 19)
cities <- c("Marseille", "Paris", "Lyon")
is_passing <- c(TRUE, FALSE, TRUE, TRUE)age <- 25 earlier, is technically just a vector of length 1.R.ages <- c(25, 31, 42, 19)
ages + 1 # adds 1 to every element
mean(ages) # average of the vector
length(ages) # number of elements[ ] to access a specific element by its position.[row, column], to access a specific element.data.frame().students <- data.frame(
name = c("Alice", "Bob", "Chloé"),
age = c(21, 23, 22),
is_erasmus = c(TRUE, FALSE, TRUE)
)
print(students)| Structure | Dimensions | Types allowed | Built with |
|---|---|---|---|
| Vector | 1D | one type | c() |
| Matrix | 2D | one type | matrix() |
| Data frame | 2D | one type per column | data.frame() |
budget containing the number 1500. Add a comment above the line explaining what it stores.name containing your first name, and a variable is_registered set to TRUE.class() to check the type of each of the three variables you just created.weekly_expenses containing four numbers of your choice.weekly_expenses, using sum() and mean().budget_tracker with two columns: item (a character vector with 3 expense categories, e.g. "Rent", "Food", "Transport") and amount (a numeric vector with 3 corresponding amounts).budget_tracker, then check the class of each of its two columns.# budget: how much money is available this month (numeric)
budget <- 1500
# name: my first name (character)
name <- "Camille"
# is_registered: whether I am registered for the course (logical)
is_registered <- TRUE
class(budget) # "numeric"
class(name) # "character"
class(is_registered) # "logical"
# A vector of 4 weekly expenses
weekly_expenses <- c(120, 95, 60, 140)
sum(weekly_expenses) # total expenses
mean(weekly_expenses) # average weekly expense
# A small data frame tracking a few budget items
budget_tracker <- data.frame(
item = c("Rent", "Food", "Transport"),
amount = c(600, 250, 80)
)
print(budget_tracker)
class(budget_tracker$item) # "character"
class(budget_tracker$amount) # "numeric"R packagesR (also true for Python)RRR language to interpret the error messages you can face and adapt the code accordinglyComparing voter turnout between French cities for the 2024 parliamentary elections.
Source: data.gouv.fr

Comparing the number of inhabitants in Aix-en-Provence over the years
Data analyses create “knowledge” out of data.
Over 89 per cent of autistic adults aged 40+ are undiagnosed.
Source: Annual Review of Developmental Psychology1, King’s College London

Data analyses are also used to take decisions.

Data analysis is one of the most important skills you can provide at the beginning of your “economist” career.
Definition: A dataset is a collection of data, typically organized in a structured format. It consists of multiple observations (rows), each described by one or more variables (columns).
| ID | Name | Age | Gender | Income |
|---|---|---|---|---|
| 1 | Alice | 30 | Female | 55000 |
| 2 | Bob | 45 | Male | 60000 |
| 3 | Charlie | 25 | Male | 50000 |
In economics, we mostly find the following data structures:
| Id | Name | University | Earnings |
|---|---|---|---|
| 1 | Alex | Harvard | 120K |
| 2 | Sarah | Yale | 100K |
| 3 | Marie | NYU | 110K |
In this example, the unit of observation is (individual).
| Id | Name | University | Application | Earnings |
|---|---|---|---|---|
| 1 | Alex | Harvard | Harvard | 120K |
| 1 | Alex | Harvard | Princeton | 120K |
| 2 | Sarah | Yale | Yale | 100K |
| 2 | Sarah | Yale | Stanford | 100K |
| 2 | Sarah | Yale | UC Berkeley | 100K |
| 3 | Marie | Princeton | Princeton | 110K |
| 3 | Marie | Princeton | Harvard | 110K |
In this example, the unit of observation is (individual, application).
| Id | Name | Univ. | Appl. 1 | Appl. 2 | Appl. 3 | Earnings | Year |
|---|---|---|---|---|---|---|---|
| 1 | Alex | Harvard | Harvard | Princeton | . | 120K | 2010 |
| 1 | Alex | Harvard | Harvard | Princeton | . | 130K | 2011 |
| 1 | Alex | Harvard | Harvard | Princeton | . | 150K | 2012 |
| 2 | Sarah | Yale | Yale | Stanford | UC Berkeley | 100K | 2010 |
| 2 | Sarah | Yale | Yale | Stanford | UC Berkeley | 100K | 2011 |
| 2 | Sarah | Yale | Yale | Stanford | UC Berkeley | 105K | 2012 |
| 3 | Marie | Princeton | Princeton | Harvard | . | 110K | 2010 |
| 3 | Marie | Princeton | Princeton | Harvard | . | 90K | 2011 |
| 3 | Marie | Princeton | Princeton | Harvard | . | 110K | 2012 |
In this example, the unit of observation is (individual, year).
Two key concepts to properly understand the comparisons we make in data analyses:
I have collected information among students with a google questionnaire on April 20, 2024.
What is the structure of my dataset?
Answer: Panel dataset
What is the unit of observation in this dataset?
Answer: A student
What comparisons can I make out of this dataset? (what are the sources of variations?)
Answer: I can compare students with each other
Let us imagine we have access to the list of all payments made by the clients of one bank.
What is the structure of my dataset?
Answer: Panel dataset
What is the unit of observation in this dataset?
Answer: A payment
What comparisons can I make out of this dataset? (what are the sources of variations?)
All three are valid sources of variation here.
Root
data
raw
tmp
out
functions
utils.R
scripts
your-script-1.R
your-script-2.R
notebooks
your-notebook-1.Rmd
your-notebook-2.Rmd
figs
tables
references
README.md
project.Rproj
"data/raw/data.csv"), R looks for that file starting from the working directory.setwd()), but best practice is to keep it at your project’s root folder.data/Root
data
raw
tmp
out
functions
utils.R
scripts
your-script-1.R
your-script-2.R
notebooks
your-notebook-1.Rmd
your-notebook-2.Rmd
figs
tables
references
README.md
project.Rproj
data: includes your data in folders:
functions/Root
data
raw
tmp
out
functions
utils.R
scripts
your-script-1.R
your-script-2.R
notebooks
your-notebook-1.Rmd
your-notebook-2.Rmd
figs
tables
references
README.md
project.Rproj
functions: folder containing the scripts with the functions you wrote.
The script files will be loaded in your analysis scripts, using the source() function.
scripts/Root
data
raw
tmp
out
functions
utils.R
scripts
your-script-1.R
your-script-2.R
notebooks
your-notebook-1.Rmd
your-notebook-2.Rmd
figs
tables
references
README.md
project.Rproj
scripts: folder containing the scripts you write to prepare the data and to perform the analysis.
notebooks/Root
data
raw
tmp
out
functions
utils.R
scripts
your-script-1.R
your-script-2.R
notebooks
your-notebook-1.Rmd
your-notebook-2.Rmd
figs
tables
references
README.md
project.Rproj
notebooks: your notebooks explaining step-by-step your methodology/codes.
figs/ and tables/Root
data
raw
tmp
out
functions
utils.R
scripts
your-script-1.R
your-script-2.R
notebooks
your-notebook-1.Rmd
your-notebook-2.Rmd
figs
tables
references
README.md
project.Rproj
figs and tables: folders containing the exported figures and tables produced by your analysis.
references/Root
data
raw
tmp
out
functions
utils.R
scripts
your-script-1.R
your-script-2.R
notebooks
your-notebook-1.Rmd
your-notebook-2.Rmd
figs
tables
references
README.md
project.Rproj
references: folder containing the PDF versions of the academic papers or documents from the grey literature used to perform your analysis.
-, best practice if you plan on sharing files on the Internet), or with underscores (_).a, aa, aaa, foo, bar, …01-prepare-data-v4.R02-merge-v2.R03-summary-statistics-v11.R04-regressions-v15.Rold folder inside your scripts folder.Root
data
raw
tmp
out
functions
scripts
notebooks
figs
tables
references
project.Rproj
Open RStudio
Create a new project
File → New Project...session-1Create the project structure
In the R console, run:
.Rproj file.R..R script?.R script is an ASCII (American Standard Code for Information Interchange) file which contains R instructions typed in plain text..R script from the graphic interface (or using the keyboard shortcut)..R script from the graphic interface or just by clicking on your .R script in the corresponding folder..R scriptR code interacting with our different subfolders..R script..R scripts# identifies what is followed until the next line as a comment. Anything written after a # will be ignored by the interpreter..R scripts for different tasks:
./scripts/01-clean-data-v1.R./scripts/02-summary-stats-v1.R./scripts/03-regressions-v1.RIn your exam, you will need to:
Let us start by finding data on Eurostat!

Introduction to programming for data analysis — Session 1
Comments: leaving notes in your code
#. Everything after#on that line is ignored byR. It is there for humans, not for the computer.Get into the habit early Add comments as you write your code, not afterward. A script without comments is much harder to read again in two weeks, even for its own author.