Extract Column of dplyr Tibble in R (Example)
This article shows how to extract a certain tibble column as vector in R.
The article will consist of the following contents:
Let’s dive right in:
Creation of Example Data
install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr package
Now, we can create an example tibble as shown below:
data <- data.frame(x1 = 1:5, # Create example tibble x2 = letters[1:5], x3 = 1) data <- as_tibble(data) data # Print example tibble # # A tibble: 5 x 3 # x1 x2 x3 # <int> <fct> <dbl> # 1 a 1 # 2 b 1 # 3 c 1 # 4 d 1 # 5 e 1
Our example tibble contains of three columns and five rows.
Example: Extract Column from tbl
If we want to select a specific column of our tibble and convert it to a vector, we can use the pull function:
x1 <- data %>% pull(x1) # Extract column from tibble x1 # Print column to console # 1 2 3 4 5
As you can see based on the RStudio console output, we extracted the column x1 from our tibble and created a new vector with the name x1.
Video & Further Resources
I have recently published a video on my YouTube channel, which illustrates the topics of this tutorial. You can find the video below.
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.
Also, you could read the related articles on my homepage. Some tutorials about dplyr and similar R packages can be found here:
- Extract Certain Columns of Data Frame
- pull R Function of dplyr Package
- Print Entire tibble to R Console
- dplyr Package Tutorial
- The R Programming Language
Summary: This tutorial illustrated how to convert a tibble variable to a vector in R programming. Let me know in the comments section, if you have any further questions. Furthermore, please subscribe to my email newsletter in order to receive regular updates on new tutorials.
Statistics Globe Newsletter