Skip to content

export vec_duplicate_split() #514

@romainfrancois

Description

@romainfrancois

For e.g. tidyverse/dplyr#4504

vec_duplicate_split() gives exactly what is needed for a vctrs based implementation of group_by() , e.g.

library(dplyr)

d <- tibble(x = c(1, 1, 1, 1, 2, 2), y = c(1, 2, 1, 2, 1, 2), z = 1:6)

vctrs:::vec_duplicate_split(select(d, x, y))
#> $key
#> [1] 1 2 5 6
#> 
#> $idx
#> $idx[[1]]
#> [1] 1 3
#> 
#> $idx[[2]]
#> [1] 2 4
#> 
#> $idx[[3]]
#> [1] 5
#> 
#> $idx[[4]]
#> [1] 6

group_by(d, x, y) %>% 
  group_data()
#> # A tibble: 4 x 3
#>       x     y .rows    
#> * <dbl> <dbl> <list>   
#> 1     1     1 <int [2]>
#> 2     1     2 <int [2]>
#> 3     2     1 <int [1]>
#> 4     2     2 <int [1]>
  • key gives the indices I can use to vec_slice() the data to get the first columns
  • idx is exactly the .rows column

tidyverse/dplyr#4504 then goes a bit further to reveal empty groups when .drop = FALSE but that does not need to be vctrs' concern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions