x <- c(1, 2, 3)
f <- function(v) v * 2
class(f) # a function is an object, exactly like a vector#> [1] "function"
class(`+`) # so is the plus sign#> [1] "function"
`+`(x, 1) # so x + 1 is a call to that object#> [1] 2 3 4
AEM 6850
Empirical Methods for Applied Economists
Prof. Ariel Ortiz-Bobea
Thursday, August 27, 2026
By the end of this session you can:
Everything that exists is an object. Everything that happens is a function call.
John Chambers, one of R’s designers
Code organised around objects: values that carry their own class, where the class decides what a function does with them.
Polymorphism means that a developer can consider a function’s interface separately from its implementation, making it possible to use the same function form for different types of input.
Hadley Wickham, Advanced R
Plainly: one name, many behaviours. You write summary(x) without knowing what x is. R reads the class and runs the version built for it.
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 1.00 1.75 2.50 26.50 27.25 100.00
#> Length Class Mode
#> 3 character character
Same call, two results. R read the class and picked the method. That is polymorphism, and it is why you learn summary(), print() and plot() once and they keep working on things you have not met yet.
| Stata | R | Python | |
|---|---|---|---|
| Built for | data analysis | data analysis | general programming |
| Data in memory | one active dataset; more via frames |
many named objects | many named objects |
| Naming the data | implicit: regress y x |
explicit: lm(y ~ x, data = d) |
explicit |
| Where tables come from | the dataset is the workspace | objects you create and name | from libraries |
| Strongest at | a clean panel to a published table | statistical methods and graphics | pipelines, APIs, ML, production code |
| Who uses it | economists, social scientists | statisticians, many fields | programmers, CS, machine learning |
Economics is a Stata field. That is measured, not folklore.
Everything in R is an object with a name you give it and a class R assigns.
Every function ships with a help page. Both of these open it, from the console:
A vector is an ordered sequence of values that are all the same class: all numbers, or all text, or all TRUE/FALSE. It is the basic unit of R: the single number 5 is a vector of length one.
Arithmetic applies to every element at once. You almost never write a loop for this:
One number out of a whole vector.
A vector can only hold one class. So when you mix classes, R does not stop or warn. It silently converts everything to a class that can hold them all.
The convenience has a cost. Here is what one stray text entry does to a column of numbers:
#> [1] "character"
#> [1] NA
Repair it, then check the repair:
NA stands for not available. It is not zero, and it is not the text "NA". It marks a value R does not have, and R enforces that: any computation touching an unknown returns an unknown.
Square brackets pull elements out by position.
Get comfortable with this one: every filter you write for the rest of the semester works this way.
A data frame is a set of equal-length vectors standing side by side: columns of possibly different classes, rows that line up. Every dataset in this course is one.
#> site date pm25
#> 1 Compton 2025-01-01 53.2
#> 2 Compton 2025-01-02 33.6
#> 3 Reseda 2025-01-01 47.0
#> 'data.frame': 3 obs. of 3 variables:
#> $ site: chr "Compton" "Compton" "Reseda"
#> $ date: chr "2025-01-01" "2025-01-02" "2025-01-01"
#> $ pm25: num 53.2 33.6 47
Subsetting takes two coordinates: d[rows, columns]. Leave one blank to mean “all of them.”
#> [1] 53.2 33.6 47.0
#> site date pm25
#> 1 Compton 2025-01-01 53.2
#> [1] 53.2 33.6 47.0
#> site date pm25
#> 1 Compton 2025-01-01 53.2
#> 3 Reseda 2025-01-01 47.0
sort() returns values. order() returns the positions that would put a vector in order.
A factor is how R stores a categorical column: the values live as numeric codes, with a lookup table of labels called levels.
Remember "10" sorting before "9"? Factors do the same thing, and it is harder to see.
Eighteen more on the session page, with solutions behind a click. One or two lines each, in the order we covered them. Work through them this week.
The session page has every command from today in one table, plus the four quick checks and the format strings for dates.
AEM 6850 · Session 2