head and tail
   get_help() docs


Description

The head() and tail() functions come with R and are part of the Base R {utils} package.

We use these functions to obtain the first (head()) or last (tail()) 6 rows of a given tibble (data frame), or more generally the first/last 6 entries in any array or list. With a second argument, you can change the default from 6 to a different value.

Conceptual Usage

head(tibble)

head(tibble, a different number of rows)

tail(tibble)

Examples

The examples below use the carnivores dataset. Learn more about this dataset with get_help("carnivores").

# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Cheetah           Acinonyx      11.9 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Northern fur seal Callorhinus   15.3 NA     
## 8 Red fox           Vulpes        14.2  0.0504
## 9 Tiger             Panthera       8.2 NA


# See first 6 rows of carnivores
head(carnivores)
## # A tibble: 6 × 4
##   name       genus        awake brainwt
##   <chr>      <fct>        <dbl>   <dbl>
## 1 Arctic fox Vulpes        11.5  0.0445
## 2 Cheetah    Acinonyx      11.9 NA     
## 3 Dog        Canis         13.9  0.07  
## 4 Gray seal  Haliochoerus  17.8  0.325 
## 5 Jaguar     Panthera      13.6  0.157 
## 6 Lion       Panthera      10.5 NA


# See last 6 rows of carnivores
tail(carnivores)
## # A tibble: 6 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Gray seal         Haliochoerus  17.8  0.325 
## 2 Jaguar            Panthera      13.6  0.157 
## 3 Lion              Panthera      10.5 NA     
## 4 Northern fur seal Callorhinus   15.3 NA     
## 5 Red fox           Vulpes        14.2  0.0504
## 6 Tiger             Panthera       8.2 NA


# See first 3 rows of carnivores
head(carnivores, 3)
## # A tibble: 3 × 4
##   name       genus    awake brainwt
##   <chr>      <fct>    <dbl>   <dbl>
## 1 Arctic fox Vulpes    11.5  0.0445
## 2 Cheetah    Acinonyx  11.9 NA     
## 3 Dog        Canis     13.9  0.07