R offers head() and tail() functions to read the first and last n rows of a dataset. Many a time you are required to work on large datasets irrespective of your analytical work using R language. It’s really hard to see or read an entire dataset at once. So, here comes the head() and tail() functions in R programming language. Let’s see how these functions work in R.

The head() function in R programming

head() function in R programming allows you to read the first n rows of the input dataset. head() function comes with a simple syntax.

Syntax:

head(data, n)

Where,

data = The input data.

X = number of rows

Great!. Now let’s see how we can use head() function to display the first n rows of the input dataset.

#Inports the datasets
df<- datasets::airquality
df
 

Well, now the Airquality dataset has over 200 rows. So it’s hard to overview the data at once. Let’s use head() function for this.

 head(df)
 
          Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6

You can observe that the head() function returned the first 6 rows of the data. This function is very helpful in overviewing the data and you can peek into it to understand the basic structure.

head() function with custom rows

Is it possible to display a specified number of rows of a dataset?. The answer is a big YES. The head() function allows you to specify the custom row number which is to be displayed.

Let’s see how it works.

head(df,4)
     Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4

This output shows only 4 rows because we have specified the count as 4. That’s it as simple as that.

Tail() function in R programming

The tail() function in R programming is the counter part of head() function which displays the last n rows of the dataset.

Tail() function’s syntax is also very simple and just like head().

Syntax:

tail(data, n)

Where,

data = The input data.

X = number of rows

Now, if you are clear with the syntax, we can move further and using tail() function we are going to display the last n rows of the input dataset.

df<- datasets::airquality
df

To make things clear and simple, I am using the same dataset. In this input data, using tail() you can get the last n rows within a second.

tail(df)
             Ozone Solar.R Wind Temp Month Day
148    14      20 16.6   63     9  25
149    30     193  6.9   70     9  26
150    NA     145 13.2   77     9  27
151    14     191 14.3   75     9  28
152    18     131  8.0   76     9  29
153    20     223 11.5   68     9  30

Fantastic!. The tail() has returned the last 6 rows present in the dataset.

Tail() function with custom rows

In this section, just like head() function, you can also add custom rows in the tail() function to get the specified data range.

Let’s see how it works!

tail(df,4)
           Ozone Solar.R Wind Temp Month Day
150    NA     145 13.2   77     9  27
151    14     191 14.3   75     9  28
152    18     131  8.0   76     9  29
153    20     223 11.5   68     9  30

It’s a very basic stuff and you can get it at a glance. Like this, you can display or read the last 4 rows or custom rows using tail() function.


The head() and tail() functions in R are the most useful functions to peek into the data and get the basic ideas of categories and data type as well.

Wrapping Up

The head() and tail() functions allow you to display the first and last n rows of the input data. You can also specify the row count if you want to display custom rows of the data. That’s all for now. Happy R!!!

Read: R docs

Categorized in:

Tagged in: