Dplyr Subset Observations Cheat Sheet

Extract rows that meet logical criteria.

dplyr::filter(iris, Sepal.Length > 7)

Remove duplicate rows.


Randomly select a fraction of rows.

dplyr::sample_frac(iris, 0.5, replace = TRUE)

Randomly select n rows.

dplyr::sample_n(iris, 10, replace = TRUE)

Select rows by position.

dplyr::slice(iris, 10:15)

Select and order top n entries (by the group if grouped data).

dplyr::top_n(storms, 2, date)