How do you extract the last row in a data frame in R?

How do you extract the last row in a data frame in R?

The process of extracting the last row in a data frame in R involves retrieving the last row of data from a data frame. This can be achieved by using the function “nrow” to determine the total number of rows in the data frame, and then using the indexing operator “[ ]” to specify the last row. The extracted row can then be assigned to a new data frame or used for further analysis. This method allows for efficient retrieval of the last row of data in R data frames.

Extract Last Row in Data Frame in R


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

Method 1: Use Base R

last_row <- tail(df, n=1)

Method 2: Use dplyr

library(dplyr)

last_row <- df %>% slice(n())

Method 3: Use data.table

library(data.table)

last_row <- setDT(df[nrow(df), ])

The following examples show how to use each method with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

Example 1: Extract Last Row Using Base R

The following code shows how to extract the last row of the data frame by using the tail() function from base R:

#extract last row in data frame
last_row <- tail(df, n=1)

#view last row
last_row

  team points assists rebounds
5    E     95      34       28

Using the tail() function, we’re able to extract only the last row in the data frame.

Note that you can change the value for the n argument to instead select the last n rows of the data frame.

Example 2: Extract Last Row Using dplyr

The following code shows how to extract the last row of the data frame by using the slice() function from the dplyr package:

library(dplyr)

#extract last row in data frame
last_row <- df %>% slice(n())

#view last row
last_row

  team points assists rebounds
1    E     95      34       28

Related:

Example 3: Extract Last Row Using data.table

The following code shows how to extract the last row of the data frame by using functions from the data.table package:

library(data.table)

#extract last row in data frame
last_row <- setDT(df[nrow(df), ])

#view last row
last_row

   team points assists rebounds
1:    E     95      34       28

Using the nrow() function, we’re able to extract only the last row in the data frame.

Cite this article

stats writer (2024). How do you extract the last row in a data frame in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-extract-the-last-row-in-a-data-frame-in-r/

stats writer. "How do you extract the last row in a data frame in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-extract-the-last-row-in-a-data-frame-in-r/.

stats writer. "How do you extract the last row in a data frame in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-extract-the-last-row-in-a-data-frame-in-r/.

stats writer (2024) 'How do you extract the last row in a data frame in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-extract-the-last-row-in-a-data-frame-in-r/.

[1] stats writer, "How do you extract the last row in a data frame in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do you extract the last row in a data frame in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top