Introducing Packages

  • Duration: ~15 Minutes
  • 5 Minute Break
Photo of the Titanic

Learning Objectives

  • What is a package?
  • How to install?
  • How to load?
  • Introducing dplyr

What is a package?

About:

  • A set of R functions.
  • Make your life easier

Examples:

How to install?


## - Always plural
## - Package name in quotes
## - Capitalization matters
install.packages("dplyr")
    

How to load?


## - Always singular
## - Quotes not needed
## - Capitalization matters
library(dplyr)
    

Introducing: dplyr

Photo of Hadley Wickham, 2016

Photo of Captain Smith Your Turn!


## Init ========================================================================
library(dplyr)
data(cars)

## Are these two lines of code equivalent? =====================================
cars[cars$speed > 20,]
filter(cars, speed > 20)
    

Answer on next slide

Yes!

Because otherwise I was going to look silly.


  speed dist
1    22   66
2    23   54
3    24   70
4    24   92
5    24   93
6    24  120
7    25   85
    

Summary Statistics Made EASY

Not enough room for Captain Smith, but run this code too.

Base R

## Easy, but limited
colMeans(cars)
        

speed  dist 
15.40 42.98 
        
dplyr

## More complex, more powerful
cars %>%
summarize(n_rows = n(),
          speed = mean(speed),
          dist = mean(dist) 
)
        

+ + + +  
 n_rows       speed      dist
1     50       15.4     42.98
        

And therein lies the rub

  • There are often multiple ways to do the same thing.
  • R is ludicrously powerful.
  • But it can also be very complex.
  • Today's goal - help you find a path forward.

5 Minute Break!

Titanic in Cobh Harbour, County Cork Ireland

Titanic in Cobh Harbour, County Cork Ireland