Skip to Content

Get First N Rows of a Dataframe in R

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 first n rows of an R dataframe with the help of some examples.

How to extract the first n rows in R?

get the first n rows from a dataframe in R

You can use the head() function in R to extract the first n rows of a dataframe. Pass the dataframe and n (the number of rows you want from the start) as arguments to the head() function.

The following is the syntax –

# get first n rows
head(dataframe, n)

If you do not pass a value for n, the head() function returns the first 6 rows of the dataframe by default.

Steps to get the first 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.

Step 2 – Extract the first n rows of dataframe using head()

First, let’s use the head() function without specifying a value for n and see what we get.

# head() with default n
print(head(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

We get the first six rows of the dataframe by default from the head() function.

Let’s now pass a custom value for n.

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

# first two rows
print(head(employees_df, 2))

Output:

    Name Age Department
1    Jim  26      Sales
2 Dwight  28      Sales

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

Summary – Extract first n rows in R

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

  1. Create a dataframe (skip this step if you already have a dataframe to operate on).
  2. Use the head() function to get the first n rows of the dataframe. Pass the number of rows you want from the top as an argument. If you do not specify n, the head() function returns the first six rows by default.

You might also be interested in –

  1. How to Create a DataFrame in R?
  2. How to Add a Row to a Dataframe in R?
  3. 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.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Tags

Tags