slice(mtcars, c(1, 2, 3))
mtcars %>% slice(1:3)
# Similar to head(mtcars, 1)
mtcars %>% slice(1L)
# Similar to tail(mtcars, 1):
mtcars %>% slice(n())
mtcars %>% slice(5:n())
# Rows can be dropped with negative indices:
slice(mtcars, -(1:4))
# First and last rows based on existing order
mtcars %>% slice_head(n = 5)
mtcars %>% slice_tail(n = 5)
# Grouped operations:
mtcars %>% group_by(am, cyl, gear) %>% slice_head(n = 2)
Run the code above in your browser using DataLab