head in R: The Complete Guide

If you have a large dataset to analyze, condensing a vast dataset with 30+ columns and thousands of rows will get tough. To solve this problem, you can use either head() or tail() function. It gives you a snapshot of that large dataset. In this tutorial, we will see how to use a head() function with different R data types.

head in R

The head() is a built-in R function that returns the first part of a Vector, Matrix, Table, data frame, or function. The head() is a generic function that can be extended in other classes. To get the first part of the object in R, use the head() function.


head(x, n, …)


The x is an object that can be a vector, Matrix, table, or data frame.

The is a positive single integer. If positive or zero, size for the resulting object: number of elements for a vector.

Implementation of R head() function

To create a data frame in R, use the data.frame() function. We passed the named vector as an argument, returning the data frame. Now, let’s fetch the first three rows of the data frame using the head() function.

df <- data.frame(c1 = c("a", "b", "c", "d"),
                 c2 = c("e", "f", "g", "h"),
                 c3 = c("i", "j", "k", "l"),
                 c4 = c("m", "n", "o", "p"),
                 c5 = c("q", "r", "s", "t"))

cat("First three rows of data frame", "\n")
head(df, 3)


   c1 c2 c3 c4 c5
1  a  e  i  m  q
2  b  f  j  n  r
3  c  g  k  o  s
4  d  h  l  p  t
First three rows of data frame
   c1 c2 c3 c4 c5
1  a  e  i  m  q
2  b  f  j  n  r
3  c  g  k  o  s

While using the head() function, we passed 3: a positive integer as an argument, which indicates the first 3 parts or, in our case, the first 3 rows of the data frame.

Using head() function on Vector

Applying the head() function to a Vector will return the number of elements equal to the n parameter of the head() function.

rv <- 1:10
cat("First five integers of vector", "\n")
head(rv, 5)


[1] 1 2 3 4 5 6 7 8 9 10
First five integers of vector
[1] 1 2 3 4 5

In this example, the n parameter of the head() function is 5, which means it will return the first five elements of the vector, which it did in the output.

Using head() function on Matrix()

To create a matrix in R, use the Matrix () function. The nrow and ncol properties define the dimension of the Matrix.

Let’s create a matrix of 5 X 3 and fetch the first 3 rows of the Matrix using the head() function.

rv <- 1:15
mtrx <- matrix(rv, nrow = 5, ncol = 3)
cat("Using head() function to get first 3 rows", "\n")
head(mtrx, 3)


     [,1] [,2] [,3]
[1,]  1    6    11
[2,]  2    7    12
[3,]  3    8    13
[4,]  4    9    14
[5,]  5   10    15
Using head() function to get first 3 rows
     [,1] [,2] [,3]
[1,]  1    6    11
[2,]  2    7    12
[3,]  3    8    13

We used the head() function and passed the Matrix and number of rows as arguments, and it returned the Matrix with the first three rows.

Using head() function on built-in dataset

We will import the USArrests inbuilt R dataset and use that dataset’s head() function.

To import the dataset as a data frame, use the following code.

df <- datasets::USArrests

Now, use the head() function to get the first five rows of the dataset.

df <- datasets::USArrests
head(df, 5)


           Murder Assault UrbanPop  Rape
Alabama    13.2    236     58       21.2
Alaska     10.0    263     48       44.5
Arizona    8.1     294     80       31.0
Arkansas   8.8     190     50       19.5
California 9.0     276     91       40.6

As you can see that we only get the first five rows of the dataset.

That is it for the head() function in R.

Related posts

How to Append Data Frames in R

How to Create Empty Data frame in R

How to Combine Two Data Frames in R

Leave a Comment