How to Remove Last Column in R


Often you may want to remove the last column from a data frame in R.

Here are the most common ways to do so:

Method 1: Remove Last Column Using length() Function

#remove last column from data frame
df_new <- df[1:(length(df)-1)]

This method removes the last column by using length(df)-1 to extract all columns minus the last column.

Method 2: Remove Last Column Using ncol() Function

#remove last column from data frame
df_new <- df[ , -seq(ncol(df), ncol(df))]

This method removes the last column by using ncol to get the number of columns in the data frame and then extract all columns except the last column.

Method 3: Remove Last Column Using tail() Function from dplyr

library(dplyr)

#remove last column from data frame
df_new <- df %>% select(-tail(names(.), 1))

This method works by using the tail() function with the names() function in dplyr to remove the last column from the data frame. Note that names() is used to get the names of the columns.

All three of these methods will produce the same result.

The following examples show how to use each of these methods in practice with the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 31, 35, 34, 45, 28, 31),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

Let’s jump in!

Example 1: Remove Last Column Using length() Function

One way to remove the last column from a data frame in R is by using the length() function with the following syntax:

#remove last column from data frame
df_new <- df[1:(length(df)-1)]

#view updated data frame
df_new

  team points assists
1    A     99      22
2    A     68      28
3    A     86      31
4    A     88      35
5    B     95      34
6    B     74      45
7    B     78      28
8    B     93      31

Notice that the new data frame named df_new contains all columns from the original data frame with the last column removed.

Example 2: Remove Last Column Using ncol() Function

Another way to remove the last column from a data frame in R is by using the ncol() function with the following syntax:

#remove last column from data frame
df_new <- df[ , -seq(ncol(df), ncol(df))]

#view updated data frame
df_new

  team points assists
1    A     99      22
2    A     68      28
3    A     86      31
4    A     88      35
5    B     95      34
6    B     74      45
7    B     78      28
8    B     93      31

Notice that the new data frame named df_new contains all columns from the original data frame with the last column removed.

Example 3: Remove Last Column Using tail() Function from dplyr

Another way to remove the last column from a data frame in R is by using the tail() function from the dplyr package with the following syntax:

library(dplyr)

#remove last column from data frame
df_new <- df %>% select(-tail(names(.), 1))

#view updated data frame
df_new

  team points assists
1    A     99      22
2    A     68      28
3    A     86      31
4    A     88      35
5    B     95      34
6    B     74      45
7    B     78      28
8    B     93      31

Notice that the new data frame named df_new contains all columns from the original data frame with the last column removed.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Add Row to Data Frame Using dplyr
How to Arrange Rows in Custom Order Using dplyr
How to Filter Based on Factor in dplyr
How to Use top_n() in dplyr

Leave a Reply

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