Extract last N rows of dataframe in R

16 Mar, 2021
The last n rows of the data frame can be accessed by using the in-built tail() method in R. Supposedly, N is the total number of rows in the data frame, then n <=N last rows can be extracted from the structure. 
Syntax:
tail(dataframe, n = ) 
Parameter : 
dataframe – The data frame to extract rows from the end
n – integer which indicates the number of rows to extract.
The changes are not retained to the original data frame. The time complexity is polynomial in terms of the number of rows to be extracted, that is the value n. 
Create dataframe
Pass required number of rows to tail()
Extract rows
Display result
Example 1:
R
# declaring data framedata_frame = data.frame(col1 = c(1:6),col2 = c(7:12),col3 = c(13:24))  # printing original data frameprint ("Original Data Frame")print(data_frame)  # extracting last row from the data framelast_row = tail(data_frame, n =1)  # printing the last row of the data frameprint ("Extracting last row from data frame")print (last_row)
Output
[1] "Original Data Frame"
  col1 col2 col3
1     1    7   13
2     2    8   14
3     3    9   15
4     4   10   16
5     5   11   17
6     6   12   18
7     1    7   19
8     2    8   20
9     3    9   21
10    4   10   22
11    5   11   23
12    6   12   24
[1] "Extracting last row from data frame"
  col1 col2 col3
12    6   12   24
Example 2:
R
# declaring data framedata_frame = data.frame(col1 = c(1:6),col2 = c(7:12),col3 = c(13:24))  # printing original data frameprint ("Original Data Frame")print(data_frame)  # extracting last row from the data framelast_4row = tail(data_frame, n = 4)  # printing the last row of the data frameprint ("Extracting last 4 rows from data frame")print (last_4row)
Output
[1] "Original Data Frame"
  col1 col2 col3
1     1    7   13
2     2    8   14
3     3    9   15
4     4   10   16
5     5   11   17
6     6   12   18
7     1    7   19
8     2    8   20
9     3    9   21
10    4   10   22
11    5   11   23
12    6   12   24
[1] "Extracting last 4 rows from data frame"
  col1 col2 col3
9     3    9   21
10    4   10   22
11    5   11   23
12    6   12   24
nrow() method can be used to extract the number of total rows in the data frame. When we pass this as the argument value of n, then all the rows are extracted. 
Example 3:
R
# declaring data framedata_frame = data.frame(col1 = c(1:3),col2 = c(7:9),col3 = c(13:15))  # printing original data frameprint ("Original Data Frame")print(data_frame)  # extracting all rows from the data frame using nrow() methoddf = tail(data_frame, n = nrow(data_frame))  # printing the last row of the data frameprint ("Extracting last rows from data frame")print (df)
Output
[1] "Original Data Frame"
 col1 col2 col3
1    1    7   13
2    2    8   14
3    3    9   15
[1] "Extracting last rows from data frame"
 col1 col2 col3
1    1    7   13
2    2    8   14
3    3    9   15
Picked
R-DataFrame
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
R - if statement
How to filter R DataFrame by values in a column?
Remove rows with NA in one column of R DataFrame