How to Delete Last X Rows of Data Frame in R (Example Code)

In this R tutorial you’ll learn how to get rid of the bottom N rows of a data frame.

Creation of Example Data

data(iris)                       # Iris data set
head(iris)                       # First six rows of iris
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

Example: Removing Bottom 135 Rows of Iris Data Frame in R

iris_top <- head(iris, - 135)    # Applying head() function
iris_top                         # Show new iris data frame
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1           5.1         3.5          1.4         0.2  setosa
# 2           4.9         3.0          1.4         0.2  setosa
# 3           4.7         3.2          1.3         0.2  setosa
# 4           4.6         3.1          1.5         0.2  setosa
# 5           5.0         3.6          1.4         0.2  setosa
# 6           5.4         3.9          1.7         0.4  setosa
# 7           4.6         3.4          1.4         0.3  setosa
# 8           5.0         3.4          1.5         0.2  setosa
# 9           4.4         2.9          1.4         0.2  setosa
# 10          4.9         3.1          1.5         0.1  setosa
# 11          5.4         3.7          1.5         0.2  setosa
# 12          4.8         3.4          1.6         0.2  setosa
# 13          4.8         3.0          1.4         0.1  setosa
# 14          4.3         3.0          1.1         0.1  setosa
# 15          5.8         4.0          1.2         0.2  setosa

Further Resources & Related Articles

In addition, you might read some of the other tutorials of my homepage.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top