everything & last_col Functions in R (2 Examples)
This post explains how to select all or the last variable using the everything and last_col functions of the tidyselect package in R programming.
The content of the article is structured as follows:
Here’s the step-by-step process…
Example Data & Software Packages
The first step is to create some data that we can use in the examples below:
data <- data.frame(x1 = 1:5, # Create example data x2 = letters[5:1], x3 = LETTERS[9:5]) data # Print example data
Have a look at the previous table. It shows that our example data consists of five rows and three columns. The variable x1 is an integer and the variables x2 and x3 have the character data type.
In order to use the functions of the tidyselect add-on package, we also have to install and load tidyselect:
install.packages("tidyselect") # Install & load tidyselect package library("tidyselect")
For this tutorial, we’ll also have to install and load the dplyr package to R:
install.packages("dplyr") # Install & load dplyr library("dplyr")
Now, we are set up! Let’s move on to the examples.
Example 1: How to Apply the everything() Function of the tidyselect Package
The following R programming syntax shows how to use the everything function of the tidyselect package.
Let’s first apply the everything function to our plain data set:
data %>% # Apply everything function everything() # [1] 1 2 3
Have a look at the previous output of the RStudio console: The everything function has returned a vector of column indices (i.e. 1, 2, and 3).
We can make use of this, for instance, when we want to reorder the columns of a data frame or tibble object.
Let’s assume that we want to move the variable x3 to the first position of our data frame:. Then, we can apply the select function in combination with the everything function as shown below:
data %>% # Reorder columns of data frame select(x3, everything()) # x3 x1 x2 # 1 I 1 e # 2 H 2 d # 3 G 3 c # 4 F 4 b # 5 E 5 a
The previous output shows a reordered version of our input data frame.
Example 2: How to Apply the last_col() Function of the tidyselect Package
Another command provided by the tidyselect package is the last_col function.
In Example 2, I’ll illustrate how to use the last_col function with the select function to extract only the last variable of a data frame.
Consider the following R code:
data %>% # Apply select & last_col functions select(last_col()) # x3 # 1 I # 2 H # 3 G # 4 F # 5 E
The previous output shows only the column at the last index position of our data frame.
Video & Further Resources
I have recently published a video on the Statistics Globe YouTube channel, which explains the R syntax of this article. You can find the video below.
The YouTube video will be added soon.
Besides that, you may read some of the related tutorials that I have published on this website. Some tutorials are listed here.
- Extract Certain Columns of Data Frame in R
- Extract Column of dplyr Tibble in R
- Extract Single Column as Data Frame in R
- Extract data.table Column as Vector Using Index Position
- Extract Values from Matrix by Column & Row Names
- Built-in R Functions
- Introduction to R
Summary: On this page, I have demonstrated how to apply the everything and last_col functions of the tidyselect package in R programming. In case you have any further comments or questions, let me know in the comments.
Statistics Globe Newsletter