In R, you can select columns (i.e., variables) from a data frame based on their names or their positions. However, if you neither know the column names nor the number of columns, keeping the last n columns can be difficult.

In this article, we discuss two methods to select the last n columns of a data frame. We will use basic R code, as well as the

last_col()
function from the dplyr package.

Example Data

In order to demonstrate how the two methods work, we will use the following dataset.

my_data <- data.frame(var1 = c(1:5),
                      var2 = letters[1:5],
                      var3 = seq(2,10,2),
                      var4 = LETTERS[22:26])

my_data

Method 1: Basic R code

The easiest way to select the last n columns of a data frame with basic R code is by combining the power of two functions. Namely,

names()
and

. tail()

These two functions have the following purpose:

By linking these two functions you easily select the last n columns of your data. Moreover, you neither need to know the actual column names, nor the number of columns.

df[tail(names(df),n)]

Here n represents the number of columns you want to select.

Example 1: Basic R code

With the R code below we select the last column of the data frame my_data.

last_column <- my_data[tail(names(my_data), 1)]
last_column

Similarly, we can use the parameter n = 2 in order to select the last two columns.

last_column <- my_data[tail(names(my_data), 2)]
last_column

Method 2: The last_col() function (dplyr)

You can also use the

last_col()
function from the dplyr package to select the last n columns from a data frame.

The last_col() function returns the name of the last column which you can use as an argument of the select() function. By combining these two functions, you easily keep the last column of a data set.

library(dplyr)

df %>%
    select(last_col())

Example 2: The last_col() function (dplyr)

This example shows how the select the last column of a data set.

last_column <- my_data %>% 
    select(last_col())

last_column

By default, the last_col() function selects only the last column. However, you can set the offset parameter to n-1 the keep the last n columns.

For example:

last_column <- my_data %>% 
    select(last_col(1):last_col())

last_column