The head() and tail() function in R – Detailed Reference

Filed Under: R Programming
The Head() And Tail() Function In R

The head() and tail() function in R are often used to read the first and last n rows of a dataset.

You may be a working professional, a programmer, or a novice learner, but there are some times where you required to read large datasets and analyze them.

It is really hard to digest a huge dataset which have 20+ columns or even more and have thousands of rows.

This article will address the head() and tail() functions in R, which returns the first and last n rows respectively.


Syntax of the head() and tail() functions

Let’s quickly see what the head() and tail() methods look like

Head(): Function which returns the first n rows of the dataset.

head(x,n=number)

Tail(): Function which returns the last n rows of the dataset.

tail(x,n=number)

Where,

x = input dataset / dataframe.

n = number of rows that the function should display.


The head() function in R

The head() function in R is used to display the first n rows present in the input data frame.

In this section, we are going to get the first n rows using head() function.

For this process, we are going to import a dataset ‘iris’ which is available in R studio by default.

#importing the dataset
df<-datasets::iris

#returns first n rows of the data
head(df)
Iris 1

You can see that the the head() function returned the first 6 rows present in the iris datatset.

The head() function with custom rows

By default, the head() function returns the first 6 rows by default.

But what if, you want to see the first 10, 15 rows if a dataset?

Well, you may observed in the syntax that you can pass the number argument to the head function to display specific number of rows.

Let’s see how it works.

#importing the data
df<-datasets::airquality

#returns first 10 rows
head(df,n=10)
Head Function In R

You can now see the head() function returned the first 10 rows as specified by us in the input. You can also write the same query as head(df,10) and get the same results.

This is how head() function works.


head() function to get first n values in the specific column

Well, in the above sections, the head() function returned the whole set of values present in the first n rows of a dataset.

But do you know that the head() function in capable to returning the values of the particular column?

Yes, you read it right!

With a single piece of code, you can get the first n values of specified column.

#importing the data
df<-datasets::mtcars

#returns first 10 values in column 'mpg'
head(mtcars$mpg,10)
Output = 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2

Just like the above sample, you can easily mention the required column name along with the required row count. That’s it.

The head() function will pierce into the data and returns the required.


The tail() function in R

The tail() function in the R is particularly used to display the last n rows of the dataset, in contrary to the head() function.

This section will illustrate the tail() function and its usage in R.

For this purpose, we are using ‘airquality’ dataset.

#importing the dataset 
df<-datasets::airquality

#returns last n rows of the data
tail(df)
Tail In R

Well, in this output, you can see the last 6 rows of the iris dataset. This is what tail() function will do in R.

The tail() function with custom rows

Similar to the head() function, the tail() function can return the last n rows of the specified count.

#importing the data
df<-datasets::airquality

#returns the last 10 values 
tail(df,10)
Tail In R 1

Here you can see, that the tail() function has returned the last 10 rows as specified by us in the code.


tail() function to get first n values in the specific column

The head() and tail() function does the same job in the quite opposite way.

You can use tail function to get last n values of a particular column as well.

Let’s see how it works!

#importing the data
df<-datasets::mtcars

#returns the last 10 values of column 'mpg'
tail(mtcars$mpg,10)
Output = 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4

If you are able to get this output, congratulations! You have done it.

Just like this sample, you can specify the column name along with row count to get the required values.


Wrapping up

The head() and tail() function in R are the most useful function when it comes to reading and analyzing the data.

You can get customized values through these functions as illustrated above. Simple syntax, effective results! – head() and tail() function in R.

That’s all for now, Happy analyzing!!!

More study: R documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages