head() and tail() Functions in R

While working with a dataframe with hundreds or thousands of rows, it can be challenging to read or print them in the R console. This guide will help you learn about the head() and tail() functions in R. For the simplicity of explanation, we will use the iris dataframe that comes preloaded with R. Let’s get started!

head() Function in R

By default, the head() function returns the first six rows of a dataframe.

Syntax

head(x, n)

Arguments

  1. x= name of the dataframe
  2. n= number of rows to be returned

Example 1: Using head() function

head(iris)

Output

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1                 5.1               3.5                1.4               0.2   setosa
2                 4.9               3.0                1.4               0.2   setosa
3                 4.7               3.2                1.3               0.2   setosa
4                 4.6               3.1                1.5               0.2   setosa
5                 5.0               3.6                1.4               0.2   setosa
6                 5.4               3.9                1.7               0.4   setosa

I’m using the head() function without the “n” argument in this example. Therefore, it will default print the first six rows of the dataframe.

Example 2: Using the head() function to print a specific number of rows

head(iris,n=2)

Output

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1                 5.1               3.5                1.4               0.2   setosa
2                 4.9               3.0                1.4               0.2   setosa

In this example, I’m passing in argument 2 inside the head() function to look at the first two rows of the dataframe.

tail() Function in R

Conversely, the tail function, by default, returns the last six rows of a dataframe.

Syntax

tail(x, n)

Arguments

  1. x= name of the dataframe
  2. n= numbers of rows to be returned

Example 1: Using head() function

tail(iris)

Output

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
145             6.7               3.3                5.7               2.5   virginica
146             6.7               3.0                5.2               2.3   virginica
147             6.3               2.5                5.0               1.9   virginica
148             6.5               3.0                5.2               2.0   virginica
149             6.2               3.4                5.4               2.3   virginica
150             5.9               3.0                5.1               1.8   virginica

I’m using the tail() function without the “n” argument in this example. Therefore, it will default print the last six rows of the dataframe.

Example 2: Using head() function to print specific number of rows

tail(iris, n=2)

Output

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
145             6.7               3.3                5.7               2.5   virginica
146             6.7               3.0                5.2               2.3   virginica

In this example, I’m adding the argument 2 inside the tail() function to look at the last two rows of the dataframe.

Leave a Comment