These functions return information about the "current" group or "current"
variable, so only work inside specific contexts like summarise()
and
mutate()
.
n()
gives the current group size.cur_group()
gives the group keys, a tibble with one row and one column for each grouping variable.cur_group_id()
gives a unique numeric identifier for the current group.cur_group_rows()
gives the row indices for the current group.cur_column()
gives the name of the current column (inacross()
only).
See group_data()
for equivalent functions that return values for all
groups.
See pick()
for a way to select a subset of columns using tidyselect syntax
while inside summarise()
or mutate()
.
data.table
If you're familiar with data.table:
cur_group_id()
<->.GRP
cur_group()
<->.BY
cur_group_rows()
<->.I
See pick()
for an equivalent to .SD
.
Examples
df <- tibble(
g = sample(rep(letters[1:3], 1:3)),
x = runif(6),
y = runif(6)
)
gf <- df %>% group_by(g)
gf %>% summarise(n = n())
#> # A tibble: 3 × 2
#> g n
#> <chr> <int>
#> 1 a 1
#> 2 b 2
#> 3 c 3
gf %>% mutate(id = cur_group_id())
#> # A tibble: 6 × 4
#> # Groups: g [3]
#> g x y id
#> <chr> <dbl> <dbl> <int>
#> 1 c 0.0367 0.642 3
#> 2 b 0.733 0.327 2
#> 3 b 0.215 0.387 2
#> 4 a 0.0160 0.708 1
#> 5 c 0.129 0.698 3
#> 6 c 0.686 0.926 3
gf %>% reframe(row = cur_group_rows())
#> # A tibble: 6 × 2
#> g row
#> <chr> <int>
#> 1 a 4
#> 2 b 2
#> 3 b 3
#> 4 c 1
#> 5 c 5
#> 6 c 6
gf %>% summarise(data = list(cur_group()))
#> # A tibble: 3 × 2
#> g data
#> <chr> <list>
#> 1 a <tibble [1 × 1]>
#> 2 b <tibble [1 × 1]>
#> 3 c <tibble [1 × 1]>
gf %>% mutate(across(everything(), ~ paste(cur_column(), round(.x, 2))))
#> # A tibble: 6 × 3
#> # Groups: g [3]
#> g x y
#> <chr> <chr> <chr>
#> 1 c x 0.04 y 0.64
#> 2 b x 0.73 y 0.33
#> 3 b x 0.22 y 0.39
#> 4 a x 0.02 y 0.71
#> 5 c x 0.13 y 0.7
#> 6 c x 0.69 y 0.93