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:
- Creation of Example Data
- Example 1: Apply pull Function with Variable Name
- Example 2: Apply pull Function with Index
- Video, Further Resources & Summary
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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may want to have a look at the related articles of https://www.statisticsglobe.com/. Some articles are listed here:
- Extract Certain Columns of Data Frame
- Extract Column of dplyr Tibble
- Convert Data Frame Column to Vector
- Overview of dplyr Tutorials
- The R Programming Language
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.
Statistics Globe Newsletter