Skip to content

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

Closed
@meowcat

Description

@meowcat

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions