Return N Top or Bottom Rows of Data Frame in R (2 Examples)
In this article you’ll learn how to print the first or last n rows of a data frame in the R programming language.
Creating Example Data
data(iris) # Loading example data |
data(iris) # Loading example data
Example 1: Applying head & tail Functions to Print First or Last Six Rows of Data Frame
head(iris) # Six rows at the top # 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 |
head(iris) # Six rows at the top # 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
tail(iris) # Six rows at the bottom # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 145 6.7 3.3 5.7 2.5 virginica # 146 6.7 3.0 5.2 2.3 virginica # 147 6.3 2.5 5.0 1.9 virginica # 148 6.5 3.0 5.2 2.0 virginica # 149 6.2 3.4 5.4 2.3 virginica # 150 5.9 3.0 5.1 1.8 virginica |
tail(iris) # Six rows at the bottom # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 145 6.7 3.3 5.7 2.5 virginica # 146 6.7 3.0 5.2 2.3 virginica # 147 6.3 2.5 5.0 1.9 virginica # 148 6.5 3.0 5.2 2.0 virginica # 149 6.2 3.4 5.4 2.3 virginica # 150 5.9 3.0 5.1 1.8 virginica
Example 2: Applying head & tail Functions to Print Any Number of Rows at Top or Bottom of Data Frame
head(iris, 10) # Ten rows at the top # 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 |
head(iris, 10) # Ten rows at the top # 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
tail(iris, 10) # Ten rows at the bottom # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 141 6.7 3.1 5.6 2.4 virginica # 142 6.9 3.1 5.1 2.3 virginica # 143 5.8 2.7 5.1 1.9 virginica # 144 6.8 3.2 5.9 2.3 virginica # 145 6.7 3.3 5.7 2.5 virginica # 146 6.7 3.0 5.2 2.3 virginica # 147 6.3 2.5 5.0 1.9 virginica # 148 6.5 3.0 5.2 2.0 virginica # 149 6.2 3.4 5.4 2.3 virginica # 150 5.9 3.0 5.1 1.8 virginica |
tail(iris, 10) # Ten rows at the bottom # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 141 6.7 3.1 5.6 2.4 virginica # 142 6.9 3.1 5.1 2.3 virginica # 143 5.8 2.7 5.1 1.9 virginica # 144 6.8 3.2 5.9 2.3 virginica # 145 6.7 3.3 5.7 2.5 virginica # 146 6.7 3.0 5.2 2.3 virginica # 147 6.3 2.5 5.0 1.9 virginica # 148 6.5 3.0 5.2 2.0 virginica # 149 6.2 3.4 5.4 2.3 virginica # 150 5.9 3.0 5.1 1.8 virginica