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

slice_sample(weight_by=) on zero-row input fails #5729

Closed
meowcat opened this issue Feb 3, 2021 · 0 comments · Fixed by #5754
Closed

slice_sample(weight_by=) on zero-row input fails #5729

meowcat opened this issue Feb 3, 2021 · 0 comments · Fixed by #5754
Milestone

Comments

@meowcat
Copy link

meowcat commented Feb 3, 2021

I am filtering a tibble and then want to sample a fraction of rows. It may well be the case that I get zero rows after filtering. In that case I expect slice_sample(prop = ???) to equally return zero rows, since 60% of zero is still zero.

This works fine as long as I don't use weight_by. If I try to weight by a column, slice_sample fails on zero-row tibbles.

Tested on CRAN and on today's github dplyr 1.0.4.9000 (Windows)

tibble(dummy = character(), weight = numeric(0)) %>% slice_sample(prop=0.5)
# A tibble: 0 x 2
# ... with 2 variables: dummy <chr>, weight <dbl>
tibble(dummy = character(), weight = numeric(0)) %>% slice_sample(prop=0.5, weight_by = weight)
# Error in sample.int(n, min(size, n), prob = wt) : 
# too few positive probabilities
romainfrancois added a commit that referenced this issue Feb 11, 2021
@romainfrancois romainfrancois added this to the 1.0.5 milestone Feb 11, 2021
romainfrancois added a commit that referenced this issue Feb 12, 2021
* work around sample.int()

closes #5729

* news
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.

2 participants