Create unique random group id in R

Maybe you can play some tricks on group_by by adding sample operation, e.g.,

iris <- iris %>%
  group_by(factor(Species, levels = sample(levels(Species)))) %>%
  mutate(id = cur_group_id())

Here's a sample answer creating a random number and ranking them.

library(datasets)
library(dplyr)

data(iris)

df <- iris %>% 
  group_by(Species) %>%
  mutate(id = runif(1,0,1)) %>% 
  ungroup() %>% 
  mutate(id = dense_rank(id))

df %>% sample_n(10)
#> # A tibble: 10 x 6
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species       id
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>      <int>
#>  1          4.4         3            1.3         0.2 setosa         3
#>  2          6.5         3            5.5         1.8 virginica      2
#>  3          6.3         2.7          4.9         1.8 virginica      2
#>  4          5           3.6          1.4         0.2 setosa         3
#>  5          6.3         2.3          4.4         1.3 versicolor     1
#>  6          7.9         3.8          6.4         2   virginica      2
#>  7          5.4         3.9          1.7         0.4 setosa         3
#>  8          5.7         4.4          1.5         0.4 setosa         3
#>  9          6.4         2.8          5.6         2.2 virginica      2
#> 10          5.2         3.4          1.4         0.2 setosa         3

Created on 2020-07-29 by the reprex package (v0.3.0)

Tags:

R

Dplyr