Skip to content

Instantly share code, notes, and snippets.

@gshotwell
Created April 3, 2020 12:57
Show Gist options
  • Save gshotwell/7ceb23ba28b1640f524424fb4f5a53d5 to your computer and use it in GitHub Desktop.
Save gshotwell/7ceb23ba28b1640f524424fb4f5a53d5 to your computer and use it in GitHub Desktop.
select_random <- function(tbl, n_records) {
tbl <- tbl %>%
dplyr::mutate(rand = random()) %>%
dplyr::compute() %>%
dplyr::arrange(.data$rand) %>%
dplyr::filter(dplyr::row_number(.data$rand) <= !!n_transactions) %>%
dplyr::select(-.data$rand)
}
tbl %>%
group_by(var) %>%
select_random(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment