How to add a counter to each group in dplyr

Hi! For people to help you it is always worth the effort to create a reproducible example. Here is more info on how to do it:

For your particular case this can be achieved with row_number function in dplyr:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
iris %>% 
  dplyr::group_by(Species) %>% 
  dplyr::sample_n(3) %>%
  dplyr::mutate(row_number = dplyr::row_number())
#> # A tibble: 9 x 6
#> # Groups:   Species [3]
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species    row_number
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>           <int>
#> 1          4.3         3            1.1         0.1 setosa              1
#> 2          5           3            1.6         0.2 setosa              2
#> 3          5.1         3.3          1.7         0.5 setosa              3
#> 4          5.5         2.4          3.8         1.1 versicolor          1
#> 5          5.9         3            4.2         1.5 versicolor          2
#> 6          5.7         2.6          3.5         1   versicolor          3
#> 7          6.5         3            5.5         1.8 virginica           1
#> 8          7.2         3.6          6.1         2.5 virginica           2
#> 9          6.7         3.3          5.7         2.1 virginica           3

Created on 2018-08-22 by the reprex package (v0.2.0).

5 Likes