head & tail Functions in R (6 Examples)

 

In this R tutorial you’ll learn how to return the first or last part of a data object using the head and tail functions.

Table of contents:

Let’s get started!

 

Creation of Example Data

We’ll use the data below as basement for this R programming tutorial:

data <- data.frame(x1 = letters[1:10],    # Create example data
                   x2 = 11:20)
data                                      # Print example data

 

table 1 data frame head tail functions

 

Have a look at the table that has been returned by the previous R syntax. It shows that our example data frame is made up of ten rows and two columns.

 

Example 1: Extract Upper Part of Data Frame Using head() Function

In Example 1, I’ll demonstrate how to apply the head function with its default specifications to return the first six rows of a data frame object.

For this, we simply have to specify the name of the data frame within the head function:

head(data)                                # Return head of data frame

 

table 2 data frame head tail functions

 

In Table 2 it is shown that we have created a new data frame output containing the top six rows of our data set by running the previous R syntax.

 

Example 2: Extract First N Rows of Data Frame Using head() Function

In Example 2, I’ll illustrate how to select a particular number of rows from the top of a data frame object.

Top achieve this, we have to specify the number of rows we want to retain in addition to the name of the data matrix:

head(data, 4)                             # Return first four rows of data frame

 

table 3 data frame head tail functions

 

By running the previously shown R syntax we have created Table 3, i.e. a data frame containing the first four rows of our input data.

 

Example 3: Extract Bottom Part of Data Frame Using tail() Function

Similar to the head function, we can use the tail function to get the last six rows of a data frame object:

tail(data)                                # Return tail of data frame

 

table 4 data frame head tail functions

 

In Table 4 you can see that we have created a data frame consisting of the last six rows of our data with the previous R code.

 

Example 4: Extract Bottom N Rows of Data Frame Using tail() Function

This example illustrates how to extract a specific number of lines from the bottom of a data matrix.

For this, we have to specify the number of rows we want to select within the tail function:

tail(data, 4)                             # Return last four rows of data frame

 

table 5 data frame head tail functions

 

Table 5 shows the output of the previous code – We have returned the bottom four rows of our data frame.

 

Example 5: Apply head() & tail() Functions to Vector Objects

So far, we have applied the head and tail functions only to data frame objects.

This example explains how to apply the head and tail functions to vector objects.

First, we have to create an example vector:

vec <- 1:10                               # Create example vector
vec                                       # Print example vector
#  [1]  1  2  3  4  5  6  7  8  9 10

Next, we can apply the head function to grab the first six elements of this vector…

head(vec)                                 # Apply head to vector object
# [1] 1 2 3 4 5 6

…or the tail command to select the last six values in our vector:

tail(vec)                                 # Apply tail to vector object
# [1]  5  6  7  8  9 10

 

Example 6: Apply head() & tail() Functions to List Objects

In Example 6, I’ll illustrate how to apply the head and tail functions to list objects.

First, let’s create an example list:

my_list <- list(1,                        # Create example list
                5:9,
                letters[6:1],
                "xxx")
my_list                                   # Print example list
# [[1]]
# [1] 1
# 
# [[2]]
# [1] 5 6 7 8 9
# 
# [[3]]
# [1] "f" "e" "d" "c" "b" "a"
# 
# [[4]]
# [1] "xxx"

Similar to the previous examples, we simply have to specify the name of this list within the head function to return the first elements (i.e. 2)…

head(my_list, 2)                          # Apply head to list object
# [[1]]
# [1] 1
# 
# [[2]]
# [1] 5 6 7 8 9

…or the last elements:

tail(my_list, 2)                          # Apply tail to list object
# [[1]]
# [1] "f" "e" "d" "c" "b" "a"
# 
# [[2]]
# [1] "xxx"

 

Video & Further Resources

Do you need further info on the R code of this tutorial? Then you may want to watch the following video on my YouTube channel. In the video, I illustrate the R programming syntax of this tutorial in RStudio.

 

The YouTube video will be added soon.

 

Furthermore, you might have a look at the other articles on this website:

 

In summary: You have learned in this post how to apply the head and tail functions in the R programming language. If you have any additional questions, please tell me about it in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top