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?
New contributor
Rooh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.