dplyr::slice()
   get_help() docs


Description

The slice() function is part of the {dplyr} package, which is part of the {tidyverse}.

It is used to subset rows from tibbles (data frames) based on indices, i.e. which row it is in the tibble.

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::slice() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::slice()

Conceptual Usage

tibble %>% 
  slice(range or index of row(s) to keep)

tibble %>% 
  slice(which, row, numbers, to, keep)

tibble %>% 
  slice(-which, -row, -numbers, -to, -remove)

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


# Keep only the 4th row from carnivores 
carnivores %>% 
  slice(4)
## # A tibble: 1 × 4
##   name      genus        awake brainwt
##   <chr>     <fct>        <dbl>   <dbl>
## 1 Gray seal Haliochoerus  17.8   0.325


# Keep only the 3rd - 5th rows from carnivores 
carnivores %>%
  slice(3:5)
## # A tibble: 3 × 4
##   name      genus        awake brainwt
##   <chr>     <fct>        <dbl>   <dbl>
## 1 Dog       Canis         13.9   0.07 
## 2 Gray seal Haliochoerus  17.8   0.325
## 3 Jaguar    Panthera      13.6   0.157


# Keep the 1st and 8th row from carnivores
carnivores %>%
  slice(1, 8)
## # A tibble: 2 × 4
##   name       genus  awake brainwt
##   <chr>      <fct>  <dbl>   <dbl>
## 1 Arctic fox Vulpes  11.5  0.0445
## 2 Red fox    Vulpes  14.2  0.0504


# Remove the 8th row from carnivores
carnivores %>%
  slice(-8)
## # A tibble: 8 × 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 Tiger             Panthera       8.2 NA


# Slice is conveniently combined with arrange() to keep, for example, "top three values of awake"
carnivores %>%
  # arrange in descending order of `awake` (large values at the top)
  arrange(desc(awake)) %>%
  slice(1:3)
## # A tibble: 3 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Gray seal         Haliochoerus  17.8  0.325 
## 2 Northern fur seal Callorhinus   15.3 NA     
## 3 Red fox           Vulpes        14.2  0.0504


# Slice is conveniently combined with arrange() to keep, for example, "BOTTOM three values of awake"
carnivores %>%
  # Arrange by default will work in ascending order (small values at the top)
  arrange(awake) %>%
  slice(1:3)
## # A tibble: 3 × 4
##   name       genus    awake brainwt
##   <chr>      <fct>    <dbl>   <dbl>
## 1 Tiger      Panthera   8.2 NA     
## 2 Lion       Panthera  10.5 NA     
## 3 Arctic fox Vulpes    11.5  0.0445