head()

head(x, n = 6L, ...)
Returns: same type as x · Updated March 13, 2026 · Base Functions
base subsetting data-frame

head() returns the first n rows of a vector, matrix, data frame, or function. It is the complement of tail(), which returns the last n elements. This function is essential for quickly inspecting large datasets without loading the entire object into memory.

Syntax

head(x, n = 6L, ...)

Parameters

ParameterTypeDefaultDescription
xobjectrequiredA vector, matrix, data frame, or function
ninteger6LNumber of elements/rows to return
...argumentsnoneAdditional arguments passed to or from other methods

Examples

Basic usage with a vector

x <- 1:20

head(x)
# [1] 1 2 3 4 5 6

head(x, n = 3)
# [1] 1 2 3

Working with a data frame

df <- data.frame(
  name = c("Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace"),
  score = c(95, 87, 92, 78, 88, 91, 85)
)

head(df)
#     name score
# 1   Alice    95
# 2     Bob    87
# 3 Charlie    92
# 4   Diana    78
# 5     Eve    88
# 6   Frank    91

Using head() with negative n

# Return all but the last n elements
head(x, n = -2)
# [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18

Common Patterns

Quick data inspection workflow

# Standard exploratory workflow
head(df)        # View first rows
tail(df)        # View last rows
str(df)         # View structure
dim(df)         # View dimensions

Pipe-friendly usage

library(dplyr)

df %>%
  head(10) %>%
  summary()

Previewing imported data

# After reading a large CSV, quickly check the first few rows
data <- read.csv("large_file.csv")
head(data)

See Also