Skip to content
Prev 388590 / 398530 Next

Assigning categorical values to dates

Hello,

Great function!
Here is a simplified, (hopefully) more general version.


seq_from_group <- function(x){
   x |>
     unique() |>
     sort() |> (\(ranks) match(x, ranks))()
}

date_df |>
   mutate(cycle = seq_from_group(dates))



With ?interaction it works with more than one column.



df2 <- data.frame(
   X = c(rep("a", 4), rep("b", 5)),
   Y = c(rep(1, 2), rep(3,3), rep(2, 4))
)

df2 |>
   mutate(cnt = seq_from_group(interaction(X, Y)))



Hope this helps,

Rui Barradas


?s 07:54 de 22/07/21, Erich Subscriptions escreveu: