Search code examples
rr-collapse

Index by group with collapse


I'm trying to create an index column based in multiple variables with a behavior like rleidv in data.table, as an example taking just a few cases of mtcars dataset

library(collapse)
mtcars |> fsubset(cyl == 4) |>
  fselect(cyl, gear, carb) |>
  roworder(cyl, gear, carb) |> 
  fgroup_by(cyl, gear) |> 
  ftransform(id = groupid(carb))

The output is as below

               cyl gear carb id
Toyota Corona    4    3    1  1
Datsun 710       4    4    1  1
Fiat 128         4    4    1  1
Toyota Corolla   4    4    1  1
Fiat X1-9        4    4    1  1
Merc 240D        4    4    2  2
Merc 230         4    4    2  2
Honda Civic      4    4    2  2
Volvo 142E       4    4    2  2
Porsche 914-2    4    5    2  2
Lotus Europa     4    5    2  2

What I want to achieve? For each combination of cyl and gear I want to make an index of the carb variable starting always in 1 , so the output should be like

               cyl gear carb id
Toyota Corona    4    3    1  1
Datsun 710       4    4    1  1
Fiat 128         4    4    1  1
Toyota Corolla   4    4    1  1
Fiat X1-9        4    4    1  1
Merc 240D        4    4    2  2
Merc 230         4    4    2  2
Honda Civic      4    4    2  2
Volvo 142E       4    4    2  2
Porsche 914-2    4    5    2  1
Lotus Europa     4    5    2  1

is there a way to do it with collapse?


Solution

  • You need fmutate instead of ftransform. From the help of ?fmutate (highlighting by me):

    Catering to the tidyverse user, v1.7.0 introduced fmutate, providing familiar functionality i.e. arguments are evaluated sequentially, computation on grouped data is done by groups, and functions can be applied to multiple columns using across. See also the Details.

    library(collapse)
    mtcars |> 
      fsubset(cyl == 4) |>
      fselect(cyl, gear, carb) |>
      roworder(cyl, gear, carb) |> 
      fgroup_by(cyl, gear) |> 
      fmutate(id = groupid(carb))
    
    #                cyl gear carb id
    # Toyota Corona    4    3    1  1
    # Datsun 710       4    4    1  1
    # Fiat 128         4    4    1  1
    # Toyota Corolla   4    4    1  1
    # Fiat X1-9        4    4    1  1
    # Merc 240D        4    4    2  2
    # Merc 230         4    4    2  2
    # Honda Civic      4    4    2  2
    # Volvo 142E       4    4    2  2
    # Porsche 914-2    4    5    2  1
    # Lotus Europa     4    5    2  1
    
    # Grouped by:  cyl, gear  [3 | 4 (3.8) 1-8]