Tutorials  /  Python

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

Ccentron Redaktion · January 2024 ·6 min read ·Python, Tutorial

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.

Code
head(x,n=number)

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

Code
tail(x,n=number)

Where,

  • x = input dataset / dataframe.
  • n = number of rows that the function should display.
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

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.

Code
#importing the dataset
    df<-datasets::iris
    #returns first n rows of the data
    head(df)

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.

Code
#importing the data
    df<-datasets::airquality
    #returns first 10 rows
    head(df,n=10)

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.

Code
#importing the data
df<-datasets::mtcars
#returns first 10 values in column 'mpg'
head(mtcars$mpg,10)
Code
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.

Code
#importing the dataset 
df<-datasets::airquality
#returns last n rows of the data
tail(df)

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.

Code
#importing the data
df<-datasets::airquality
#returns the last 10 values
tail(df,10)

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!

Code
#importing the data
df<-datasets::mtcars
#returns the last 10 values of column 'mpg'
tail(mtcars$mpg,10)
Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren