What Does Head Do In R

5 min read

The head() function in R is one of the most frequently used tools for quickly inspecting the beginning of a data object. Whether you are working with a vector, a data frame, a list, or a matrix, head() returns the first few elements or rows, allowing you to peek at your data without printing thousands of values to the console. Understanding what does head do in R is essential for efficient data exploration, debugging, and sharing reproducible examples. This article explains the function’s purpose, syntax, behavior across different data structures, practical use cases, and common pitfalls—so you can use head() confidently in your daily R workflow.

Understanding the head() Function in R

At its core, head() extracts the first part of an object. The default number of elements returned is 6, a sensible choice that gives you enough context without flooding your screen. The basic syntax is:

head(x, n = 6L, ...)
  • x: the object you want to inspect (vector, data frame, list, matrix, function, etc.).
  • n: a single integer specifying how many elements (or rows) to return. Positive values return the first n elements; negative values return all but the last |n| elements.
  • ...: additional arguments passed to methods (rarely needed for beginners).

The function is generic, meaning it has specialized methods for different classes. This is why head() works without friction on data frames, tibbles, and even custom objects.

Why Use head() in R?

The primary reason to use head() is brevity. When you load a large dataset—say, a CSV with 100,000 rows—printing the entire object is impractical and slow. head() gives you a snapshot of the first few rows, including column names and data types (for data frames), so you can verify that the data was imported correctly.

Most guides skip this. Don't.

Other benefits include:

  • Quick sanity checks: Confirm that column names, values, and formats look as expected.
  • Debugging: Inspect intermediate results in a pipeline without overwhelming output.
  • Documentation: Share a small, reproducible snippet of your data in reports or help requests.
  • Performance: Avoid memory and time overhead from printing massive objects.

In short, head() is the go-to function for the first step of exploratory data analysis.

How head() Works with Different Data Structures

Vectors

For atomic vectors (numeric, character, logical), head() returns the first n elements. If n exceeds the vector length, it returns the entire vector.

numbers <- c(10, 20, 30, 40, 50, 60, 70, 80)
head(numbers)        # returns 10 20 30 40 50 60
head(numbers, 3)     # returns 10 20 30
head(numbers, -2)    # returns all but the last two: 10 20 30 40 50 60

Data Frames

For data frames, head() returns the first n rows with all columns. This is the most common use case.

head(mtcars)         # first 6 rows of the built-in mtcars dataset
head(mtcars, 10)     # first 10 rows

The output includes row names (if any) and column headers, making it easy to spot missing values or unexpected data types Small thing, real impact..

Lists

When applied to a list, head() returns a sublist containing the first n components.

my_list <- list(a = 1:5, b = "text", c = TRUE, d = 3.14)
head(my_list, 2)     # returns $a and $b

Matrices

For matrices, head() returns the first n rows, preserving the matrix structure.

mat <- matrix(1:20, nrow = 10)
head(mat, 3)         # first 3 rows, all columns

Functions and Other Objects

head() also works on functions (returning the first lines of the function body) and on arbitrary objects via the default method. This flexibility makes it a universal inspection tool Practical, not theoretical..

Practical Examples of head() in Action

Let’s walk through a few realistic scenarios.

Example 1: Inspecting a built-in dataset

head(iris)

This returns the first six rows of the famous iris dataset, showing sepal length, sepal width, petal length, petal width, and species. You immediately see that the data is clean and properly labeled Turns out it matters..

Example 2: Checking a custom vector

scores <- c(88, 92, 79, 85, 95, 70, 81, 89, 94, 76)
head(scores, 4)

Output: 88 92 79 85. This confirms the first four scores without printing all ten.

Example 3: Using negative n

head(scores, -3)

This returns all scores except the last three: 88 92 79 85 95 70 81. Negative n is handy when you want to drop trailing elements but keep the rest.

Example 4: Combining head() with other functions

head(sort(scores, decreasing = TRUE), 3)

This returns the top three scores: 95 94 92. You can nest head() inside pipelines or other function calls for concise analysis Worth keeping that in mind..

Common Mistakes and Best Practices

Even though head() is simple, a few pitfalls can trip up beginners.

  • Forgetting that n defaults to 6: If you need more or fewer rows, always specify n explicitly. Relying on the default

...Relying on the default can result in printing more or fewer rows than intended, especially when the function is called across different datasets in a script. To avoid ambiguity, it's best practice to always pass n explicitly, for example head(x, 10) instead of relying on the six-row default.

Another consideration involves the behavior when n exceeds the object's length. In such cases, head() simply returns the entire object without error, which can be convenient for quick checks but may mask issues if you're specifically sampling a subset. Similarly, when n is set to 0,

Fresh from the Desk

Just Shared

Based on This

Keep Exploring

Thank you for reading about What Does Head Do In R. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home