You are currently viewing R head() & tail() Functions

What is the head() and tail() functions do in R? The head() in R is used to get the first rows of the DataFrame, Vector, or compatible object. The tail() is used to get the last rows of the DataFrame, Vector, or a compatible object.

  • head() – Returns the first few elements from the object and the object can be a vector, matrix, table, or data frame.
  • tail() – Returns the last few elements from the object and the object can be a vector, matrix, table, or data frame.

1. R head() Function

The head() function is used to get the first few elements from the DataFrame or the first few elements from the Vector. It takes two arguments the input object and the number of rows you wanted to get from the first.

1.1 Syntax of head()

The following is the syntax of the head() function.


# Syntax
head(x,n=number)

1.2 head() Example in R

In the following example, I have demonstrated getting the first few rows from the DataFrame. The first example doesn’t take the number as input hence, it returns the default count. And the second example takes 3 as the number hence, it returns the first 3 rows from the DataFrame


# head() example
head(df)

# head with number
head(df,3)

Yields below output.

head in r

2. R tail() Function

The R tail() function is used to get the last few elements from the DataFrame or the last few elements from the Vector. It takes two arguments the input object and the number of rows you wanted to get from the last.

2.1 Syntax of tail()

The following is the syntax of the tail() function.


# Syntax
tail(x,n=number)

2.2 tail() Example in R

In the following example, I have demonstrated getting the last few rows from the DataFrame. The first example doesn’t take the number as input hence, it returns the default count. And the second example takes 3 as the number hence, it returns the last 3 rows from the DataFrame


#tail
tail(df)

#tail with size
tail(df,3)

3. Vector head() & tail()

In all the above examples, we have seen getting head and tail rows from the R DataFrame. These methods can also be used to get the first and last elements from the R Vector.

Let’s create the R character Vector.


# Create vector
vec <- c('Scott','Ram','Deepika','Sravan','Ann','Hans','Jeff','Harly')

# head of the Vector
head(vec)
head(vec,4)

# tail of the Vector
tail(vec)
tail(vec,4)

Yields below output.

4. R matrix head() & tail()

Let’s see how to get the head() and tail() of the matrix object in R.


# Create R matrix
data <- c(10,11,12,13,14,15,16,17,18)
mtx <- matrix(data,nrow=3,ncol=3,byrow=TRUE)
mtx

#head() matrix
head(mtx)
head(mtx,4)

#tail() matrix
tail(mtx)
tail(mtx,4)

4. Complete Example

Following is a complete example of using head() and tail() functions in R.


# Create DataFrame
df <- data.frame(
  id = c(10,11,12,13,18,20,25,40),
  name = c('Scott','Ram','Deepika','Sravan','Ann','Hans','Jeff','Harly'),
  dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))
)

#head
head(df)

#head with size
head(df,3)

#tail
tail(df)

#tail with size
tail(df,3)

# Create vector
vec <- c('Scott','Ram','Deepika','Sravan','Ann','Hans','Jeff','Harly')

# head of the Vector
head(vec)
head(vec,4)

# tail of the Vector
tail(vec)
tail(vec,4)

4. Conclusion

In this article, you have learned what head() and tail() functions do in R. The head() function is used to get the first rows and the tail is used to get the last rows of objects DataFrame, Vector, Matrix, and List.

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium