How to use dplyr pull in R

06.05.2021

Intro

The pull method from dplyr allows use to simplify accessing single values from data. We can see this by comparing the base r way with the dplyr way. In this article, we will learn how to use the dplyr pull function in R.

If you are in a hurry

If you don’t have time to read, here is a quick code snippet for you.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --

## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.0     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1

## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
mtcars %>% pull(mpg)
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4

Loading the Library

We can load the dplyr package directly, but I recommend loading the tidyverse package as we will use some other features in side.

library(tidyverse)

Loading the Dataset

For this tutorial, we will use the mtcars data set the comes with tidyverse. We take a look at this data set below.

data(mtcars)

glimpse(mtcars)
## Rows: 32
## Columns: 11
## $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,~
## $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,~
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16~
## $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180~
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,~
## $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.~
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18~
## $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,~
## $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,~
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,~
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,~

The Basic dplyr Pull

To use the pull method, we can pass the data set we would like to extract from and the name of the column we would like to extract.

pull(mtcars, mpg)
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4

However, it is more common to use the pipe, %>%, operator when working with the tidyverse. To use this, we can pipe our data to the pull method. Here is a rewrite of the example above.

mtcars %>% pull(mpg)
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4

Comparing the Pull Method to Others

Let’s look at how we would accomplish the same in base R. First, we have two options. We can use the [[]] double bracket operator or the $ operator. Both of these will extract a vector from data. We compare this again to the pull method below.

mtcars[['mpg']]
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
mtcars$mpg
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
mtcars %>% pull(mpg)
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4

So, the main benefit here is the consistent language (the verbs, i.e. pull) you get when using tidyverse, but let’s continue on to see some other benefits of pull.

Quickly, let’s compare pull to select. If you know how to use select, you would normally use the single bracket [] operator in R. This will usually return a data frame instead of a vector.

mtcars['mpg']
##                      mpg
## Mazda RX4           21.0
## Mazda RX4 Wag       21.0
## Datsun 710          22.8
## Hornet 4 Drive      21.4
## Hornet Sportabout   18.7
## Valiant             18.1
## Duster 360          14.3
## Merc 240D           24.4
## Merc 230            22.8
## Merc 280            19.2
## Merc 280C           17.8
## Merc 450SE          16.4
## Merc 450SL          17.3
## Merc 450SLC         15.2
## Cadillac Fleetwood  10.4
## Lincoln Continental 10.4
## Chrysler Imperial   14.7
## Fiat 128            32.4
## Honda Civic         30.4
## Toyota Corolla      33.9
## Toyota Corona       21.5
## Dodge Challenger    15.5
## AMC Javelin         15.2
## Camaro Z28          13.3
## Pontiac Firebird    19.2
## Fiat X1-9           27.3
## Porsche 914-2       26.0
## Lotus Europa        30.4
## Ford Pantera L      15.8
## Ferrari Dino        19.7
## Maserati Bora       15.0
## Volvo 142E          21.4
mtcars %>% select(mpg)
##                      mpg
## Mazda RX4           21.0
## Mazda RX4 Wag       21.0
## Datsun 710          22.8
## Hornet 4 Drive      21.4
## Hornet Sportabout   18.7
## Valiant             18.1
## Duster 360          14.3
## Merc 240D           24.4
## Merc 230            22.8
## Merc 280            19.2
## Merc 280C           17.8
## Merc 450SE          16.4
## Merc 450SL          17.3
## Merc 450SLC         15.2
## Cadillac Fleetwood  10.4
## Lincoln Continental 10.4
## Chrysler Imperial   14.7
## Fiat 128            32.4
## Honda Civic         30.4
## Toyota Corolla      33.9
## Toyota Corona       21.5
## Dodge Challenger    15.5
## AMC Javelin         15.2
## Camaro Z28          13.3
## Pontiac Firebird    19.2
## Fiat X1-9           27.3
## Porsche 914-2       26.0
## Lotus Europa        30.4
## Ford Pantera L      15.8
## Ferrari Dino        19.7
## Maserati Bora       15.0
## Volvo 142E          21.4

Again, compare this with pull.

 mtcars %>% pull(mpg)
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4

Accessing From the Left

With the pull method, we can also supply a numeric offset for which column we would like to access. Here are two examples of accessing the first and third columns.

mtcars %>% pull(1)
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
mtcars %>% pull(3)
##  [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8
## [13] 275.8 275.8 472.0 460.0 440.0  78.7  75.7  71.1 120.1 318.0 304.0 350.0
## [25] 400.0  79.0 120.3  95.1 351.0 145.0 301.0 121.0

Accessing From the Right

Similar to the above, we can access from the right. Instead of passing a positive number, we will pass a negative number. Here is an example of accessing the last and the third from the last columns.

mtcars %>% pull(-1)
##  [1] 4 4 1 1 2 1 4 2 2 4 4 3 3 3 4 4 4 1 2 1 1 2 2 4 2 1 2 2 4 6 8 2
mtcars %>% pull(-3)
##  [1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1

Multipe Columns

Pull does support multiple columns, but returns a different result than using select. See here for example of pull height and name from the starwars data set.

starwars %>% pull(height, name)
##        Luke Skywalker                 C-3PO                 R2-D2 
##                   172                   167                    96 
##           Darth Vader           Leia Organa             Owen Lars 
##                   202                   150                   178 
##    Beru Whitesun lars                 R5-D4     Biggs Darklighter 
##                   165                    97                   183 
##        Obi-Wan Kenobi      Anakin Skywalker        Wilhuff Tarkin 
##                   182                   188                   180 
##             Chewbacca              Han Solo                Greedo 
##                   228                   180                   173 
## Jabba Desilijic Tiure        Wedge Antilles      Jek Tono Porkins 
##                   175                   170                   180 
##                  Yoda             Palpatine             Boba Fett 
##                    66                   170                   183 
##                 IG-88                 Bossk      Lando Calrissian 
##                   200                   190                   177 
##                 Lobot                Ackbar            Mon Mothma 
##                   175                   180                   150 
##          Arvel Crynyd Wicket Systri Warrick             Nien Nunb 
##                    NA                    88                   160 
##          Qui-Gon Jinn           Nute Gunray         Finis Valorum 
##                   193                   191                   170 
##         Jar Jar Binks          Roos Tarpals            Rugor Nass 
##                   196                   224                   206 
##              Ric Olié                 Watto               Sebulba 
##                   183                   137                   112 
##         Quarsh Panaka        Shmi Skywalker            Darth Maul 
##                   183                   163                   175 
##           Bib Fortuna           Ayla Secura              Dud Bolt 
##                   180                   178                    94 
##               Gasgano        Ben Quadinaros            Mace Windu 
##                   122                   163                   188 
##          Ki-Adi-Mundi             Kit Fisto             Eeth Koth 
##                   198                   196                   171 
##            Adi Gallia           Saesee Tiin           Yarael Poof 
##                   184                   188                   264 
##              Plo Koon            Mas Amedda          Gregar Typho 
##                   188                   196                   185 
##                 Cordé           Cliegg Lars     Poggle the Lesser 
##                   157                   183                   183 
##       Luminara Unduli         Barriss Offee                 Dormé 
##                   170                   166                   165 
##                 Dooku   Bail Prestor Organa            Jango Fett 
##                   193                   191                   183 
##            Zam Wesell       Dexter Jettster               Lama Su 
##                   168                   198                   229 
##               Taun We            Jocasta Nu         Ratts Tyerell 
##                   213                   167                    79 
##                R4-P17            Wat Tambor              San Hill 
##                    96                   193                   191 
##              Shaak Ti              Grievous               Tarfful 
##                   178                   216                   234 
##       Raymus Antilles             Sly Moore            Tion Medon 
##                   188                   178                   206 
##                  Finn                   Rey           Poe Dameron 
##                    NA                    NA                    NA 
##                   BB8        Captain Phasma         Padmé Amidala 
##                    NA                    NA                   165