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:
-
- df – Data frame
- n – number of rows
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
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 be
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.
Syntax for tail function in R:
- df – Data frame
- n – number of rows
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
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
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.
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
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
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
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
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
Slice by Group in R:
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)
slice_tail() by group in R:
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)
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)
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
for more details refer here