For data exploration, quickly inspecting the data, verifying the structure, or debugging large datasets, we need some kind of tool that helps prevent excessive data flooding in the console. That’s where head() and tail() generic functions come into play.
The head() function displays the first n parts of the vector, matrix, table, or data frame. If you don’t pass the count “n” or rows to be displayed, it will display six rows/elements by default.
head(data, n = number, ...)
Name | Value |
data | An input object (data frame, vector, matrix, or list) |
n | Number of rows/elements to be returned.
|
… | They are additional arguments. For example, if you use matrices, you can use nrow or ncol. |
If the “n” argument exceeds the input object’s length, it will return the whole object.
Let’s define a data frame and use the head() function to quickly inspect the data frame.
# Creating a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
head(df)
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
# Selecting first 3 rows
head(df, 3)
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
# Selecting first 4 rows of column 3
head(df$col2, 4)
If you pass the negative “n”, it will start excluding the rows from the end of the data frame. For example, if you want to exclude the last five rows from a data frame, you should pass n = -5.
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
# excluding last 2 rows
head(df, -5)
It will return the following output:
col1 col2 col3
1 1 8 15
2 2 9 16
The above output shows that it removed the last five rows and displayed the first two rows.
df <- datasets::USArrests
head(df, 5)
Output
rv <- 1:5
cat("First three values of vector", "\n")
head(rv, 3)
Let’s create a matrix of 7×3 and fetch the first four rows of the Matrix.
rv <- 1:21
mtrx <- matrix(rv, nrow = 7, ncol = 3)
cat("Using head() function to get first 4 rows", "\n")
head(mtrx, 4)
Let’s define a list and quickly view the first 2 elements of the list:
# Creating a list
demo_list <- list(a = 1:3, b = 4:6, c = 7:9, d = 10:12)
demo_list
# $a
# [1] 1 2 3
# $b
# [1] 4 5 6
# $c
# [1] 7 8 9
# $d
# [1] 10 11 12
# Get first 2 elements
head(demo_list, n = 2)
# $a
# [1] 1 2 3
# $b
# [1] 4 5 6
The tail() function views the last n part of a vector, matrix, table, or data frame.
It complements the head() function that returns the first n rows of a data frame.
tail(data, n, ...)
Name | Value |
data | An input object (data frame, vector, matrix, or list). |
n | Number of rows/elements to be returned.
|
… | They are additional arguments. For example, if you use matrices, you can use nrow or ncol. |
If the “n” argument exceeds the input object’s length, it will return the whole object.
By default, it will return the last six rows of the data frame if you have not passed a number of rows/elements.
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
# Applying tail() function to df
tail(df)
To select the last three rows of a data frame, pass three as the second argument.
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
# Selecting last 3 rows using tail() function
tail(df, 3)
Let’s implement the above diagram in R code.
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
# Selecting last 4 rows of column 3
tail(df$col3, 4)
If you pass the negative n, it will start excluding rows from the start. For example, tail(df, -5) will exclude the first five rows from the data frame.
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3, 4, 5, 6, 7),
col2 = c(8, 9, 10, 11, 12, 13, 14),
col3 = c(15, 16, 17, 18, 19, 20, 21)
)
print(df)
# excluding first 5 rows
tail(df, n = -5)
Output
col1 col2 col3
6 6 13 20
7 7 14 21
As you can see, it removes the first five rows and displays the last two rows.
Let’s import the ChickWeight dataset.
df <- datasets::ChickWeight
Use the tail() function to get the last five rows of the dataset.
df <- datasets::ChickWeight
tail(df, 5)
Output
weight Time Chick Diet
574 175 14 50 4
575 205 16 50 4
576 234 18 50 4
577 264 20 50 4
578 264 21 50 4
rv <- 1:10
rv
cat("Last five integers of vector", "\n")
tail(rv, 5)
Output
[1] 1 2 3 4 5 6 7 8 9 10
Last five integers of vector
[1] 6 7 8 9 10
rv <- 1:21
mtrx <- matrix(rv, nrow = 7, ncol = 3)
cat("Using tail() function to get last 3 rows", "\n")
tail(mtrx, 3)
# Creating a list
demo_list <- list(a = 1:3, b = 4:6, c = 7:9, d = 10:12)
demo_list
# $a
# [1] 1 2 3
# $b
# [1] 4 5 6
# $c
# [1] 7 8 9
# $d
# [1] 10 11 12
# Get last 2 elements
tail(demo_list, n = 2)
# $c
# [1] 7 8 9
# $d
# [1] 10 11 12
That’s it!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…
The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…
The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…
The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…
In R, you can use the dollar sign ($ operator) to access elements (columns) of…
The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…