Group a data frame and add in-group indices as a column.

add_group_sub_indices(
  .data,
  ...,
  groupingVars,
  orderColumn,
  arrangeTerms = NULL,
  env = parent.frame()
)

Arguments

.data

data.frame

...

force later arguments to bind by name.

groupingVars

character vector of column names to group by.

orderColumn

character name of column to add in-group order marks to.

arrangeTerms

character vector of column expressions to arrange by.

env

environment to work in.

Value

.data with in group order indices added (no ties).

Examples

groupingVars = c("cyl", "gear") datasets::mtcars %.>% # dplyr doesn't currently export tibble::rownames_to_column() mutate_se(., "CarName" := "rownames(.)" ) %.>% select_se(., c('CarName', 'cyl', 'gear', 'hp', 'wt')) %.>% add_group_indices(., groupingVars = groupingVars, indexColumn = 'groupID') %.>% add_group_sub_indices(., groupingVars = groupingVars, arrangeTerms = c('desc(hp)', 'wt'), orderColumn = 'orderInGroup') %.>% arrange_se(., c('groupID', 'orderInGroup'))
#> # A tibble: 32 x 7 #> CarName cyl gear hp wt groupID orderInGroup #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 Toyota Corona 4 3 97 2.46 1 1 #> 2 Volvo 142E 4 4 109 2.78 2 1 #> 3 Merc 230 4 4 95 3.15 2 2 #> 4 Datsun 710 4 4 93 2.32 2 3 #> 5 Fiat X1-9 4 4 66 1.94 2 4 #> 6 Fiat 128 4 4 66 2.2 2 5 #> 7 Toyota Corolla 4 4 65 1.84 2 6 #> 8 Merc 240D 4 4 62 3.19 2 7 #> 9 Honda Civic 4 4 52 1.62 2 8 #> 10 Lotus Europa 4 5 113 1.51 3 1 #> # … with 22 more rows