pull R Function of dplyr Package (2 Examples)

 

This article explains how to extract columns with the pull function of the dplyr add-on package in R programming.

Table of contents:

Let’s dive right in!

 

Creation of Example Data

In the examples of this R tutorial, we’ll use the following data frame:

data <- data.frame(x1 = 1:5,            # Create example data
                   x2 = LETTERS[1:5])
data                                    # Print example data
#   x1 x2
# 1  1  A
# 2  2  B
# 3  3  C
# 4  4  D
# 5  5  E

Our data frame contains five rows and two columns. Note that we could also use a tibble of the tidyverse.

Furthermore, we also have to install and load the dplyr R package:

install.packages("dplyr")               # Install dplyr
library("dplyr")                        # Load dplyr

 

Example 1: Apply pull Function with Variable Name

The pull function can be applied in two different ways. The first way is by specifying the name of the variable that we want to extract within the pull function:

pull(data, x1)                          # Apply pull function
# 1 2 3 4 5

As you can see based on the output of the RStudio console, the pull function returned our column with the name x1 as vector.

 

Example 2: Apply pull Function with Index

The second way how we can apply the pull function is by specifying the index of the variable that we want to extract:

pull(data, 1)                           # pull function with index
# 1 2 3 4 5

The previous R code returns the first column of our data frame, i.e. the output is the same as in Example 1.

 

Video, Further Resources & Summary

If you need more information on the R codes of this tutorial, you might want to watch the following video of my YouTube channel. In the video, I show the R syntax of this tutorial:

 

The YouTube video will be added soon.

 

In addition, you may want to have a look at the related articles of https://www.statisticsglobe.com/. Some articles are listed here:

 

This tutorial showed how to select and pull variables from data frames and tibbles with the dplyr package in R. If you have additional questions, don’t hesitate to let me know in the comments section.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top