Back to Browse

R programming | Using slice() function of dplyr to select rows

143 views
Jan 13, 2024
5:02

Dplyr is a data manipulation package that is part of the tidyverse universe, which is a collection of libraries that has the goal of making R faster, simpler and easier. Dplyr leverages the pipe structure, a better way to encapsulate functions. The library has dozens of functions available to perform data manipulation and wrangling. Slice provides a convenient way to include specific rows or ranges of rows in your datasets. library(tidyverse) #create dataset df = data.frame(team=c('A', 'A', 'A', 'B', 'B', 'C', 'C'), points=c(1, 2, 3, 4, 5, 6, 7), assists=c(1, 5, 2, 3, 2, 2, 0)) #view dataset df #Method 1: Subset One Specific Row #get row 3 only df pipeline slice(3) #Method 2: Subset Several Rows #get rows 2, 5, and 6 df pipeline slice(2, 5, 6) #Method 3: Subset A Range of Rows #get rows 1 through 3 df pipeline slice(1:3) #Method 4: Subset Rows by Group #get first row by group df pipeline group_by(team) pipeline slice(1) #rprogramming #datascience #rstudio #slice #dplyr #rdatacode #tidyverse #rdatacode

Download

0 formats

No download links available.

R programming | Using slice() function of dplyr to select rows | NatokHD