A dataframe in R is a data structure used to store data in a tabular form – in rows and columns. In this tutorial, we will look at how to get the last n rows of an R dataframe with the help of some examples.

## How to extract the last n rows in R?

You can use the `tail()`

function in R to extract the last n rows of a dataframe. Pass the dataframe and n (the number of rows you want from the end) as arguments to the `tail()`

function.

The following is the syntax –

# get last n rows tail(dataframe, n)

If you do not pass a value for `n`

, the `tail()`

function returns the last 6 rows of the dataframe by default.

## Steps to get the last n rows of a dataframe in R

Let’s now look at a step-by-step example to show the usage of the syntax mentioned above.

### Step 1 – Create a dataframe

First, we will create a dataframe that we will be using throughout this tutorial.

# create a dataframe employees_df = data.frame( "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin", "Meredith", "Creed"), "Age"= c(26, 28, 29, 32, 30, 38, 49), "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting", "QA", "QA") ) # display the dataframe print(employees_df)

Output:

Name Age Department 1 Jim 26 Sales 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting 6 Meredith 38 QA 7 Creed 49 QA

We now have a dataframe containing some information about employees in an office. The dataframe has the columns – “Name”, “Age”, and “Department”. Note that the dataframe above has 7 rows.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

### Step 2 – Extract the last n rows of dataframe using `tail()`

First, let’s use the `tail()`

function without specifying a value for n and see what we get.

# tail() with default n print(tail(employees_df))

Output:

Name Age Department 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting 6 Meredith 38 QA 7 Creed 49 QA

We get the last six rows (row 2 to row 7) of the dataframe by default from the `tail()`

function.

Let’s now pass a custom value for n.

For example, let’s just get the last two rows of the above dataframe.

# last two rows print(tail(employees_df, 2))

Output:

Name Age Department 6 Meredith 38 QA 7 Creed 49 QA

Here, we specify n as 2 and thus we get the last two rows of the dataframe.

## Summary – Extract last n rows in R

In this tutorial, we looked at how to get the last n rows of a dataframe in R. The following is a short summary of the steps mentioned in this tutorial.

- Create a dataframe (skip this step if you already have a dataframe to operate on).
- Use the
`tail()`

function to get the last n rows of the dataframe. Pass the number of rows you want from the end as an argument. If you do not specify n, the`tail()`

function returns the last six rows by default.

You might also be interested in –

- How to Create a DataFrame in R?
- How to Add a Row to a Dataframe in R?
- Rename Column Name in R Dataframe

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**