Skip to content

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

@KWB4484

Description

@KWB4484

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions