How do I remove the last row in a data frame using dplyr?

To remove the last row from a data frame using dplyr, you can use the filter() function to exclude the last row. This can be done by filtering out the values which are not equal to the last value of a certain column in the data frame. For example, if the last value of the column “ID” in the data frame is 5, then you can remove the last row by using the code dataframe %>% filter(ID != 5).


You can use the following methods to remove the last row from a data frame in R:

Method 1: Remove Last Row from Data Frame

library(dplyr)

#remove last row from data frame
df <- df %>% filter(row_number() <= n()-1)

Method 2: Remove Last N Rows from Data Frame

library(dplyr)

#remove last four rows  from data frame
df <- df %>% filter(row_number() <= n()-4)

Note: The n() function extracts the total number of rows in the data frame.

By using row_number() <= n(), we are specifying that we’d like to filter the data frame to only contain rows where the row number is less than the total number of rows with some number subtracted.

The following examples show how to use each of these methods in practice with the following data frame:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'),
                 points=c(18, 13, 19, 14, 24, 21, 20, 28),
                 assists=c(5, 7, 17, 9, 12, 9, 5, 12))

#view data frame
df

  team points assists
1    A     18       5
2    A     13       7
3    A     19      17
4    B     14       9
5    B     24      12
6    C     21       9
7    C     20       5
8    C     28      12

Example 1: Remove Last Row from Data Frame

The following code shows how to remove the last row from the data frame:

library(dplyr)

#remove last row from data frame
df <- df %>% filter(row_number() <= n()-1)

#view updated data frame
df

  team points assists
1    A     18       5
2    A     13       7
3    A     19      17
4    B     14       9
5    B     24      12
6    C     21       9
7    C     20       5

Notice that the last row of the data frame has been removed.

Example 2: Remove Last N Rows from Data Frame

The following code shows how to remove the last four rows from the data frame:

library(dplyr)

#remove last four rows from data frame
df <- df %>% filter(row_number() <= n()-4)

#view updated data frame
df

  team points assists
1    A     18       5
2    A     13       7
3    A     19      17
4    B     14       9

Notice that the last four rows of the data frame have been removed.

The following tutorials explain how to perform other common functions in dplyr:

x