R- Manipulating Rows: filter/ Slice

filter()

  • Used to subset or filter rows based on specific conditions.
  • It retains all rows that satisfy the condition
  • To be retained, all the values in the row must evaluate to TRUE

Syntax:

Useful filter functions

There are many functions and operators that are useful when constructing the expressions used to filter the data:

Example 1

Example 2

Example 3: filter using the %in%

Example 3

Slice

  • Extract rows in a dataset by its position
  • slice() lets you index rows by their (integer) locations
  •  It allows you to select, remove, and duplicate rows.

Slice Helper Functions

  • slice_head() and slice_tail() select the first or last rows.
  • slice_sample() randomly selects rows.
  • slice_min() and slice_max() select rows with the smallest or largest values of a variable.

slice_min(
.data,
order_by,
…,
n,
prop,
by = NULL,
with_ties = TRUE,
na_rm = FALSE
)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top