How to Remove Last Row in Data Frame Using dplyr


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.

Note: To remove a different number of rows from the end of the data frame, simply change the 4 in the code to a different number.

Additional Resources

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

How to Select Columns by Index Using dplyr
How to Rank Variables by Group Using dplyr
How to Replace NA with Zero in dplyr

Leave a Reply

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