Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group_by() slice_sample() does not work when n is greater than the number of rows in the group #6271

Closed
KWB4484 opened this issue May 18, 2022 · 0 comments · Fixed by #6282
Closed

Comments

@KWB4484
Copy link

KWB4484 commented May 18, 2022

According to dplyr 1.0.9 slice() documentation "If n is greater than the number of rows in the group (or prop > 1), the result will be silently truncated to the group size." However this is not true if using group_by() and slice_sample().

df <- tibble(
  group = rep(c("a", "b", "c"), c(1, 2, 4)),
  x = runif(7)
)


df %>% group_by(group) %>% slice_sample(n = 2)

Error in slice_sample(): ! Problem while computing indices. i The
error occurred in group 1: group = "a". Caused by error in
sample.int(): ! cannot take a sample larger than the population when
'replace = FALSE' Run rlang::last_error() to see where the error
occurred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant