Examples for 'dplyr::context'


Context dependent expressions

Aliases: context n cur_data cur_data_all cur_group cur_group_id cur_group_rows cur_column

Keywords:

### ** 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.0414 0.639      3
2 b     0.470  0.963      2
3 a     0.233  0.0999     1
4 c     0.424  0.0674     3
5 c     0.0799 0.469      3
6 b     0.0740 0.518      2
gf %>% summarise(row = cur_group_rows())
`summarise()` has grouped output by 'g'. You can override using the `.groups`
argument.
# A tibble: 6 × 2
# Groups:   g [3]
  g       row
  <chr> <int>
1 a         3
2 b         2
3 b         6
4 c         1
5 c         4
6 c         5
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 %>% summarise(data = list(cur_data()))
# A tibble: 3 × 2
  g     data            
  <chr> <list>          
1 a     <tibble [1 × 2]>
2 b     <tibble [2 × 2]>
3 c     <tibble [3 × 2]>
gf %>% summarise(data = list(cur_data_all()))
# A tibble: 3 × 2
  g     data            
  <chr> <list>          
1 a     <tibble [1 × 3]>
2 b     <tibble [2 × 3]>
3 c     <tibble [3 × 3]>
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.47 y 0.96
3 a     x 0.23 y 0.1 
4 c     x 0.42 y 0.07
5 c     x 0.08 y 0.47
6 b     x 0.07 y 0.52

[Package dplyr version 1.0.9 Index]