Get first n rows & last n rows – head(), tail(), slice(),top_n() function in R

In This section we will learn about head and tail function in R. head() function in R takes argument “n” and returns the first n rows of a dataframe or matrix, by default it returns first 6 rows. tail() function  in R returns last n rows of a dataframe or matrix, by default it returns last 6 rows. we can also use slice() group of functions in dplyr package like slice_sample(),slice_head(), slice_tail(), slice_min() and slice_max() function to get n rows. top_n() function can also be used for same. Lets see an example of

  • Head Function in R: returns the first n rows of a matrix or data frame in R
  • Tail Function in R: returns the last n rows of a matrix or data frame in R
  • slice_sample() function in R returns the sample n rows of the dataframe in R
  • slice_max() function in R returns the maximum n rows of the dataframe in R
  • slice_min() function in R returns the minimum n rows of the dataframe in R
  • First n rows is returned using slice_head() function in R
  • Last n rows is returned using slice_tail() function in R
  • top_n() function in R returns the top n rows based on a specific column.

 

Syntax for head function in R:

head(df)
head(df,n=number)
    • df – Data frame
    • n – number of rows

head and tail function in R 1

 

Example of head function in R:

Lets use mtcars table to demonstrate head function in R

# head function in R

head(mtcars)

By default head function in R returns first 6 rows of a data frame or matrix so the output will be
head() and tail() function in r slice(),top_n() 1

 

Example of head function in R with Specified rows:

# head function in R with specified rows

head(mtcars, n=2)

head function in R returns first 2 rows of a data frame or matrix so the output will behead() and tail() function in r slice(),top_n() 2

 

head() function to extract first n values of a column :

head() function takes up the column name and number of values to be extracted as argument as show below.

 

# head function in R to get first n values of the column

head(mtcars$mpg, n=10)

so the first n values of the mpg column is extracted as shown below.

[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2

 

 

Syntax for tail function in R:

tail(df)
tail(df,n=number)
  • df – Data frame
  • n – number of rows

head and tail function in R 2

Example of tail function in R:

Lets use mtcars table to demonstrate tail function in R

# tail function in R

tail(mtcars)

By default tail function in R returns last 6 rows of a data frame or matrix so the output will be
head() and tail() function in r slice(),top_n() 3

Example of tail function in R with Specified rows:

# tail function in R with specified rows

tail(mtcars, n=2)

tail function in R returns last 2 rows of a data frame or matrix so the output will be
head() and tail() function in r slice(),top_n() 4

tail() function to extract last n values of a column :

tail() function takes up the column name and number of values to be extracted as argument as show below.

 

# tail function in R to get first n values of the column
tail(mtcars$mpg, n=10)

so the last n values of the mpg column is extracted as shown below.

[1] 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4

 

 

 


Slice Family of function in R dplyr :

slice_head() function in R 

slice_head() function returns the top n rows of the dataframe as shown below.

 
# slice_head() function in R
library(dplyr)
mtcars %>% slice_head(n = 5)

so the top 5 rows are returned

head() and tail() function in r slice(),top_n() 5

 

slice_tail() function in R:

slice_tail() function returns the bottom n rows of the dataframe as shown below.

 
# slice_tail() function in R
library(dplyr) 
mtcars %>% slice_tail(n = 5)

so the sample 5 rows are returned

head() and tail() function in r slice(),top_n() 6

 

slice_max() function in R: 

slice_max() function returns the maximum n rows of the dataframe based on a column as shown below.

 
# slice_max() function in R
library(dplyr) 
mtcars %>% slice_max(mpg, n = 5)

so the max 5 rows based on mpg column will be returned
head() and tail() function in r slice(),top_n() 8

 

slice_min() function in R: 

slice_min() function returns the minimum n rows of the dataframe based on a column as shown below.

 
# slice_min() function in R

library(dplyr) 
mtcars %>% slice_min(mpg, n = 5)

so the min 5 rows based on mpg column will be returned
head() and tail() function in r slice(),top_n() 7

 

 

slice_sample() function in R: 

slice_sample() function returns the sample n rows of the dataframe as shown below.

 
# slice_sample() function in R

library(dplyr) 
mtcars %>% slice_sample(n = 5)

so the sample 5 rows are returned

head() and tail() function in r slice(),top_n() 9

 


Slice by Group in R:

head and tail function in R 5

 

 

slice_head() by group in R:  returns the top n rows of the group using slice_head() and group_by() functions

 

# slice_head() by group in R 
mtcars %>% group_by(vs) %>% slice_head(n = 2)

head() and tail() function in r slice(),top_n() 10b

 

slice_tail() by group in R:

head and tail function in R 5

slice_tail() by group in R  returns the bottom n rows of the group using slice_tail() and group_by() functions

 

# slice_tail() by group in R 
mtcars %>% group_by(vs) %>% slice_tail(n = 2)

head() and tail() function in r slice(),top_n() 11

 

slice_sample() by group in R:

slice_sample() by group in R  Returns the sample n rows of the group using slice_sample() and group_by() functions

 

# slice_sample() by group in R 
mtcars %>% group_by(vs) %>% slice_sample(n = 2)

head() and tail() function in r slice(),top_n() 12

 

 


Using top_n() function in R:

Top n rows of the dataframe with respect to a column is achieved by using top_n() functions

 

# top_n() function in R 

mtcars %>% top_n(10)

so the resultant dataframe will be

head() and tail() function in r slice(),top_n() 13

for more details refer here


Other Related Topics :

previous small head and tail function in r                                                                                                           next small head and tail function in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts