EC 320 - Introduction to Econometrics
2025
In this lecture, you will:
Good news! If you are reading this, you are most likely on a computer already. Great work so far.
The good news about R is that it works on any operating system. I personally use it on both Mac and Windows.
Go to https://cran.r-project.org/ and follow the instructions to download R for your device.
Go to https://posit.co/download/rstudio-desktop/ and click the blue button that says step 2: install Rstudio Desktop. Follow the instructions to complete the installation.
Run these lines of code in your console to make sure you have the tidyverse
installed and attached to your current session.
An important shortcut to installing and using packages is a package called pacman
. It has a function p_load()
which functions as both install.packages()
and library()
above.
Use p_load
to install the next set of packages
A former PhD Student (Colleen O’Briant) in the Econ Department created a lovely package that serves as an alternative set of beginner friendly help docs.
Run this code to install it:
To test everything worked, now run:
If everything went right, the help docs she wrote on the function install.packages
should pop up.
Note: It is not a complete library of every function, but many of the basic ones we will use are included.
Vectors
Pipes
In R, data is held in vectors. You can construct a vector using the function c()
. c
is short for “combine”.
You can combine elements to form a vector, for example:
The pipe %>%
is the most frequently used function in the tidyverse
.
What It Does:
Suppose you have some data x
and you would like to apply some function f()
on it. So you run f(x)
.
min()
:The pipe simply takes the data that comes before it and inserts it into the function that comes after
The way you should read the pipe is with the word “then”, as in: “take x
, then apply f()
”
We are not limited to only one pipe. What if we wanted to take x
then apply f()
, then apply g()
, then apply h()
.
Using pipes:
Or using multiple lines (this is easier to read once things get hectic)
If we did not use pipes, we’d have to read it inside-out h(g(f(x)))
, which is nasty.
This worksheet will help you learn coding by doing. You will learn:
min()
and sum()
work on vectorslength()
to find the number of elements in a vectorrep()
EC320, R Lecture | Vectors and Pipes