Skip to content
Prev 388592 / 398530 Next

Assigning categorical values to dates

While the base R solution using 'factor' appears to win based on elegance,
chapeau to the creativity of the other suggestions.
For those who are not aware, R 4.1.0 introduced two features: (1) native
pipe |> and (2) new shorter syntax for anonymous functions.
Erich's suggestion used the native pipe and Rui went with the spirit and
added an anonymous function using the new syntax.

Everyone has their preferred coding style. I tend to prefer fewer lines of
code (if there is no cost in understanding).
I think the new anonymous function syntax helps in this regard and I see no
reason to use piping if not necessary.
So here is a modified, one-line version of Rui's last suggestion (sans the
amazing observation about handling interactions).

mutate(date_df, cycle=(\(ranks) match(dates, ranks))(sort(unique(dates))))

Eric




On Thu, Jul 22, 2021 at 11:11 AM Uwe Ligges <ligges at statistik.tu-dortmund.de>
wrote: