R-Lang
R Basic
R Advanced
R Charts
tail() in R: How to Get Last Part of R Object
June 8, 2022 May 29, 2022 by Krunal Lathiya
If you have a large dataset to analyze, condensing a huge dataset with 30+ columns and thousands of rows will get tough. To solve this problem, you can use the head() or tail() function. It gives you a snapshot of that large dataset. In this tutorial, we will see how to use a tail() function with different R data types.
tail in R
The tail() is a built-in R function that returns the last part of a vector, Matrix, table, data frame, or function. The tail() function accepts an R object and optional arguments like a single positive integer. The tail() is a generic function that can be extended in other classes.
To get the last part of the object in R, use the tail() function.
tail(x, n, …)
The x is an object that can be a vector, Matrix, table, or data frame.
The n is a positive single integer. If positive or zero, size for the resulting object: number of elements for a vector.
Implementation of R tail() 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 last three rows of the data frame using the tail() 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("Last two rows of data frame", "\n")
tail(df, 2)
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
Last two rows of data frame
While using the tail() function, we passed 2: a positive integer as an argument, which indicates the last 2 parts or, in our case, the first two rows of the data frame.
Using tail() function on Vector
Applying the tail() function on a Vector will return the number of elements equal to the n parameter of the tail() function.
rv <- 1:10
cat("Last five integers of vector", "\n")
tail(rv, 5)
[1] 1 2 3 4 5 6 7 8 9 10
Last five integers of vector
[1] 6 7 8 9 10
In this example, the n parameter of the tail() function is 5, which means it will return the last five elements of the vector, which it did in the output.
Using tail() function on Matrix()
To create a matrix in R, use the Matrix () function. The dimension of the Matrix is defined by nrow and ncol properties.
Let's create a matrix of 5 X 3 and fetch the last 2 rows of the Matrix using the tail() function.
mtrx <- matrix(rv, nrow = 5, ncol = 3)
mtrx
cat("Using tail() function to get the last 2 rows", "\n")
tail(mtrx, 2)
[1,]  1    6    11
[5,]  5   10    15
Using tail() function to get the last 2 rows
[4,]  4    9   14
[5,]  5   10   15
In this example, we used the tail() function and passed the Matrix and number of rows as arguments, and it returns the Matrix with the last two rows.
Using tail() function on built-in dataset
We will import the ChickWeight inbuilt R dataset and use the tail() function on that dataset.
To import the dataset as a data frame, use the following code.
df <- datasets::ChickWeight
Now, use the tail() function to get the last five rows of the dataset.
weight Time Chick Diet
574   175   14    50    4
As you can see that we only get the last five rows of the dataset.
That is it for the tail() function in R.
view in R
sample in R
remove rows in R
Krunal Lathiya
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.
Categories R Basic
head in R: The Complete Guide
Standard deviation in R: A Beginner's Guide
© 2023 R-lang All rights reserved.