Skip to content

tidy grouped data attributes #3489

Closed
Closed
@romainfrancois

Description

@romainfrancois

Would it make sense to tidy attributes that we use internally for grouped data frame, we could e.g. have labels, indices and indices sizes in the same data frame, which would be convenient for manipulation because then it's tidy. (inspired by working on #341).

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
df <- data.frame(x = 1:2, y = factor(c(1, 1), levels = 1:2))
gdf <- group_by(df, x, y)

attributes(gdf)[ c("labels", "indices", "group_sizes")]
#> $labels
#>   x y
#> 1 1 1
#> 2 2 1
#> 
#> $indices
#> $indices[[1]]
#> [1] 0
#> 
#> $indices[[2]]
#> [1] 1
#> 
#> 
#> $group_sizes
#> [1] 1 1
as_tibble(
  mutate( attr(gdf, "labels"), 
    ..index.. = attr(gdf, "indices"), 
    ..size.. = attr(gdf, "group_sizes")
  )
)
#> # A tibble: 2 x 4
#>       x y     ..index.. ..size..
#>   <int> <fct> <list>       <int>
#> 1     1 1     <int [1]>        1
#> 2     2 1     <int [1]>        1

Created on 2018-04-09 by the reprex package (v0.2.0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions