The slice() function from the dplyr package is used to subset the R data.frame rows (observations) based on index/position/number and also used to drop the rows and duplicate the rows by Index. Using this doesn’t modify the columns, groups, and data frame attributes. By providing negative values it will drop the specified rows (In other words it just excludes the rows specified with a negative position).
dplyr is an R package that offers a grammar for data manipulation and includes a set of commonly used verbs to assist data science analysts in performing typical data manipulation tasks. To use dplyr, install it install.packages('dplyr')
and load it with library(dplyr)
. In this article, you will learn the syntax of the slice()
function, its arguments, and its usage with examples.
Slice Verbs | Description |
---|---|
slice() | Slices the data.frame by row index |
slice_head() | Select the first rows |
slice_tail() | Select the last rows |
slice_min() | Select the minimum of a column |
slice_max() | Select the maximum of a column |
slice_random() | Select random rows |
1. Syntax of slice()
Following is the syntax of the R dplyr::slice()
function.
# Syntax of slice()
slice(.data, ..., .preserve = FALSE)
Arguments
.data
– data.frame or tibble to slice....
– Specify how you want to slice.preserve
–
2. Data Preparation
Let’s create an R data frame, run these examples,
# Create DataFrame
df <- data.frame(
id = c(10,11,12,13,14,15,16,17),
name = c('sai','ram','deepika','sahithi','kumar','scott','Don','Lin'),
gender = c('M','M','F','F','M','M','M','F'),
dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16',
'1995-03-02','1991-6-21','1986-3-24','1990-8-26')),
state = c('CA','NY',NA,NA,'DC','DW','AZ','PH'),
row.names=c('r1','r2','r3','r4','r5','r6','r7','r8')
)
df
Yields below output.
# Output
id name gender dob state
r1 10 sai M 1990-10-02 CA
r2 11 ram M 1981-03-24 NY
r3 12 deepika F 1987-06-14 <NA>
r4 13 sahithi F 1985-08-16 <NA>
r5 14 kumar M 1995-03-02 DC
r6 15 scott M 1991-06-21 DW
r7 16 Don M 1986-03-24 AZ
r8 17 Lin F 1990-08-26 PH
3. Slice Rows by Index Position
Using dplyr::slice()
function in R lets you select the rows (observations) from data.frame by index position (row number). This function takes the first argument as data.frame, the second argument with all indexes by comma separator you wanted to select. Refer to how to select rows from the data frame for other ways of getting data from data frame.
# Load dplyr library
library('dplyr')
# Select rows 2 and 3
df2 <- df %>% slice(2,3)
df2
# Output
# id name gender dob state
#r2 11 ram M 1981-03-24 NY
#r3 12 deepika F 1987-06-14 <NA>
4. Slice Rows by List of Index Positions
If you have a list of indexes you want to slice from the data frame use the vector with the indexes as a second argument. The following example selects rows from indexes 2,3,5, and 6.
# Select rows from list
df2 <- df %>% slice(c(2,3,5,6))
df2
# Output
# id name gender dob state
#r2 11 ram M 1981-03-24 NY
#r3 12 deepika F 1987-06-14 <NA>
#r5 14 kumar M 1995-03-02 DC
#r6 15 scott M 1991-06-21 DW
5. Slice by start and end position (by range)
It also supports range with start and end index positions separated by :
. The following example slices the R data.frame rows between indexes 2 and 6 range.
# select rows by range
df2 <- df %>% slice(2:6)
df2
# Output
# id name gender dob state
#r2 11 ram M 1981-03-24 NY
#r3 12 deepika F 1987-06-14 <NA>
#r4 13 sahithi F 1985-08-16 <NA>
#r5 14 kumar M 1995-03-02 DC
#r6 15 scott M 1991-06-21 DW
6. Drop Rows
slice() function also lets you drop rows from the r data frame by supplying negative index values as arguments. The first example from below demonstrates dropping by index position and the second example demonstrates dropping by the range of negative index position.
# Drop rows using slice()
df2 <- df %>% slice(-2,-3,-4,-5,-6)
df2
# Drop by range
df2 <- df %>% slice(-2:-6)
df2
# Both yields same output
# id name gender dob state
#r1 10 sai M 1990-10-02 CA
#r7 16 Don M 1986-03-24 AZ
#r8 17 Lin F 1990-08-26 PH
7. Complete Example of slice() Function
The following is the complete example of the usage of the slice() function in R.
# Create DataFrame
df <- data.frame(
id = c(10,11,12,13,14,15,16,17),
name = c('sai','ram','deepika','sahithi','kumar','scott','Don','Lin'),
gender = c('M','M','F','F','M','M','M','F'),
dob = as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16',
'1995-03-02','1991-6-21','1986-3-24','1990-8-26')),
state = c('CA','NY',NA,NA,'DC','DW','AZ','PH'),
row.names=c('r1','r2','r3','r4','r5','r6','r7','r8')
)
df
# Load dplyr library
library('dplyr')
# Select rows 2 and 3
df2 <- df %>% slice(2,3)
df2
# Select rows from list
df2 <- df %>% slice(c(2,3,5,6))
df2
# select rows by range
df2 <- df %>% slice(2:6)
df2
# Drop rows using slice()
df2 <- df %>% slice(-2,-3,-5,-6)
df2
# Drop by range
df2 <- df %>% slice(-2:-6)
df2
8. Conclusion
In this article, you have learned the slice() function of the R dplyr package. This function is used to subset the R data.frame rows based on index/position/number. Using this doesn’t modify the columns, groups, and data frame attributes. By providing negative values it will drop the specified rows (In other words it just excludes the rows specified with a negative position)
Related Articles
- R subset() function
- R filter() function from dplyr package
- R select() function from dplyr package
- R mutate() function from dplyr package
- How to select rows by name in R?
- How to select rows by column Value in R?
- How to select rows by condition in R?
- How to subset dataframe by column value in R?
- How to filter dataframe by column value?
References
- https://dplyr.tidyverse.org/reference/slice.html
- https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/slice