Learning R: Vectors and Pipes

EC 320 - Introduction to Econometrics

Jose Rojas-Fallas

2025

Preview

In this lecture, you will:

  • Download R and RStudio
  • Install some packages
  • Learn how to create vectors and use pipes to apply functions
  • Complete some exercises

Installing R and RStudio

Step 1: Grab a Computer

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.

Step 2: Download and Install R

Go to https://cran.r-project.org/ and follow the instructions to download R for your device.

Step 3: Install RStudio

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.

Step 4: Open RStudio and Install Some Packages

Run these lines of code in your console to make sure you have the tidyverse installed and attached to your current session.

install.packages("tidyverse", dependencies = TRUE)
library(tidyverse)


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.

install.packages("pacman")

Use p_load to install the next set of packages

Step 4a: Install More 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:

library(pacman)
p_load(Rcpp, devtools) # Note you can do several packages at once

install_github("cobriant/qelp")

To test everything worked, now run:

?qelp::install.packages

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.

Tidied Data

Topics

  1. Vectors

  2. Pipes

Vectors

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:

  • Combine numbers to form a numeric vector:
c(5,3,9)
[1] 5 3 9
  • Combine character strings to form a character vector:
c("apple", "banana", "strawberry")
[1] "apple"      "banana"     "strawberry"

Note: character strings need quotes around them. numbers should not have quotes around them

Pipes

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).

  • For example, take the vector 1:3 and find its minimum by applying min():
min(1:3)
[1] 1

Another way to do the same things is to use a pipe:

1:3 %>% min()
[1] 1

Pipes

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:

x %>% f() %>% g() %>% h()

Or using multiple lines (this is easier to read once things get hectic)

x %>%
    f() %>% # Benefit: Allows you to comment on each function to keep track of what you are doing
    g() %>% # Hello
    h() # Goodbye

If we did not use pipes, we’d have to read it inside-out h(g(f(x))), which is nasty.

Practice: Download “Worksheet 01” From the Site

This worksheet will help you learn coding by doing. You will learn:

  • Math operators work on vectors
  • min() and sum() work on vectors
  • How to use length() to find the number of elements in a vector
  • Repeat elements using rep()
  • How to do Random Sampling from a vector
  • How to pipe data through multiple functions