Open In App

Extract last N rows of dataframe in R

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Approach

  • Create dataframe
  • Pass required number of rows to tail()
  • Extract rows
  • Display result

Example 1:

R




# declaring data frame
data_frame = data.frame(
col1 = c(1:6),
col2 = c(7:12),
col3 = c(13:24))
  
# printing original data frame
print ("Original Data Frame")
print(data_frame)
  
# extracting last row from the data frame
last_row = tail(data_frame, n =1)
  
# printing the last row of the data frame
print ("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 frame
data_frame = data.frame(
col1 = c(1:6),
col2 = c(7:12),
col3 = c(13:24))
  
# printing original data frame
print ("Original Data Frame")
print(data_frame)
  
# extracting last row from the data frame
last_4row = tail(data_frame, n = 4)
  
# printing the last row of the data frame
print ("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 frame
data_frame = data.frame(
col1 = c(1:3),
col2 = c(7:9),
col3 = c(13:15))
  
# printing original data frame
print ("Original Data Frame")
print(data_frame)
  
# extracting all rows from the data frame using nrow() method
df = tail(data_frame, n = nrow(data_frame))
  
# printing the last row of the data frame
print ("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


Last Updated : 16 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads