Wilcar
Wilcar

Reputation: 2513

dplyr sample by groups of values

I want to make samples based on grouped values with dplyr :

What I tried :

 id <- c(1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8)

 id <-  as.data.frame(id)

 sample <- id %>%
   group_by(id) %>%
   sample_n(2, replace = FALSE) %>%
   ungroup(id)

sample

Expected result ( n sample =2) :

1, 1, 1, 2

or

1, 1, 1, 3, 3

or

5, 5, 5, 6, 6

etc.

I have got an error:

Error: `size` must be less or equal than 1 (size of data), set `replace` = TRUE to use sampling with replacement

Upvotes: 3

Views: 4054

Answers (1)

akrun
akrun

Reputation: 887841

Perhaps this helps

id %>% 
  distinct(id) %>% 
  sample_n(2, replace = FALSE) %>% 
  inner_join(id, .)

Upvotes: 3

Related Questions