The head() and tail() function in R - Detailed Reference | DigitalOcean (2024)

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 datasetdf<-datasets::iris#returns first n rows of the datahead(df)

The head() and tail() function in R - Detailed Reference | DigitalOcean (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 datadf<-datasets::airquality#returns first 10 rowshead(df,n=10)

The head() and tail() function in R - Detailed Reference | DigitalOcean (2)

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 datadf<-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 datatail(df)

The head() and tail() function in R - Detailed Reference | DigitalOcean (3)

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 datadf<-datasets::airquality#returns the last 10 values tail(df,10)

The head() and tail() function in R - Detailed Reference | DigitalOcean (4)

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 datadf<-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

The head() and tail() function in R - Detailed Reference  | DigitalOcean (2024)

FAQs

What is 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.

What is the usage of head () and tail () in data management? ›

To view a small sample of a Series or DataFrame object, use the head() and tail() methods. The default number of elements to display is five, but you may pass a custom number. of elements to display is five, but you may pass a custom number.

What is the head () function? ›

The head() function is used to get the first n rows. This function returns the first n rows for the object based on position. It is useful for quickly testing if your object has the right type of data in it.

What is tail command in R? ›

tail() function in R Language is used to get the last parts of a vector, matrix, table, data frame or function. Syntax: tail(x, n)

What does head () mean in R? ›

The head() function in R is used to determine the first or last part of an R object.

What are the head and tail? ›

1. : this side or that side. often used in plural in tossing a coin to decide a choice, question, or stake compare heads or tails. : beginning or end : one thing or another : something definite.

Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 5973

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.