Table of Contents
Dplyr is a popular R package that offers a streamlined and efficient approach to data manipulation. One common task in data analysis is removing rows from a data frame. Using dplyr, this can be achieved by using the `slice()` function with the negative index of the last row. This removes the last row from the data frame and returns the updated data frame. This method is convenient and efficient for removing the last row in a data frame using dplyr.
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 framedf <- df %>% filter(row_number() <= n()-1)
Method 2: Remove Last N Rows from Data Frame
library(dplyr) #remove last four rows from data framedf <- 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 framedf <- 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 framedf <- 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:
Cite this article
stats writer (2024). How can I remove the last row in a data frame using dplyr?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-the-last-row-in-a-data-frame-using-dplyr/
stats writer. "How can I remove the last row in a data frame using dplyr?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-the-last-row-in-a-data-frame-using-dplyr/.
stats writer. "How can I remove the last row in a data frame using dplyr?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-the-last-row-in-a-data-frame-using-dplyr/.
stats writer (2024) 'How can I remove the last row in a data frame using dplyr?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-the-last-row-in-a-data-frame-using-dplyr/.
[1] stats writer, "How can I remove the last row in a data frame using dplyr?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I remove the last row in a data frame using dplyr?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
