How to Use slice in dplyr for Row Selection in R
In
dplyr, use slice() to select rows by their position in a data frame or tibble. You provide row numbers or ranges inside slice() to keep only those rows. It is useful for picking specific rows without filtering by values.Syntax
The basic syntax of slice() is:
slice(data, rows): Selects rows by their position.data: Your data frame or tibble.rows: Numeric indices or ranges indicating which rows to keep.
You can use positive numbers to select rows or negative numbers to exclude rows.
r
slice(data, 1:3) # Select rows 1 to 3 slice(data, c(2, 5)) # Select rows 2 and 5 slice(data, -1) # Exclude the first row
Example
This example shows how to use slice() to select specific rows from a tibble.
r
library(dplyr) # Create example data my_data <- tibble( name = c("Alice", "Bob", "Carol", "David", "Eva"), age = c(25, 30, 22, 35, 28) ) # Select rows 2 to 4 selected_rows <- slice(my_data, 2:4) print(selected_rows)
Output
# A tibble: 3 ร 2
name age
<chr> <dbl>
1 Bob 30
2 Carol 22
3 David 35
Common Pitfalls
Common mistakes when using slice() include:
- Using filtering conditions instead of row positions (use
filter()for conditions). - Passing row numbers that are out of range, which causes errors.
- Confusing
slice()withslice_head()orslice_tail()which select rows from start or end.
Example of wrong and right usage:
r
library(dplyr) my_data <- tibble(x = 1:5) # Wrong: trying to filter rows with condition inside slice (this will cause error) # slice(my_data, x > 3) # Incorrect # Right: use filter for conditions filter(my_data, x > 3) # Right: use slice with row positions slice(my_data, 4:5)
Output
# A tibble: 2 ร 1
x
<int>
1 4
2 5
Quick Reference
| Function | Description | Example Usage |
|---|---|---|
| slice() | Select rows by position | slice(data, 1:3) |
| slice_head() | Select first n rows | slice_head(data, n = 3) |
| slice_tail() | Select last n rows | slice_tail(data, n = 2) |
| slice_min() | Select rows with smallest values | slice_min(data, order_by = col) |
| slice_max() | Select rows with largest values | slice_max(data, order_by = col) |
Key Takeaways
Use slice() to select rows by their numeric position in a data frame or tibble.
Provide row numbers or ranges inside slice() to keep specific rows.
Do not use slice() for filtering by values; use filter() instead.
Negative numbers in slice() exclude rows by position.
Check row indices to avoid out-of-range errors.