context | R Documentation |
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 (in across()
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()
.
n()
cur_group()
cur_group_id()
cur_group_rows()
cur_column()
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
.
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())
gf %>% mutate(id = cur_group_id())
gf %>% reframe(row = cur_group_rows())
gf %>% summarise(data = list(cur_group()))
gf %>% mutate(across(everything(), ~ paste(cur_column(), round(.x, 2))))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.