Message-ID: <f78079ce-f183-1bff-7c1a-5acff3481883@sapo.pt>
Date: 2021-07-22T07:49:22Z
From: Rui Barradas
Subject: Assigning categorical values to dates
In-Reply-To: <B6FC6FE6-8272-4CE5-AA72-334FEECA7679@neuwirth.priv.at>
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:
> date_df <- tibble(dates = c(rep("2021-07-04", 2), rep("2021-07-25", 3),
> rep("2021-07-18", 4)))
>
>
>
> cycle_from_date <- function(date,dates){
> dates |>
> unique() |>
> sort() ->
> ranks
> match(date,ranks)
> }
>
> date_df |>
> mutate(cycle_new=cycle_from_date(dates,dates))
>
>
>> On 22.07.2021, at 05:46, N. F. Parsons <nathan.f.parsons at gmail.com> wrote:
>>
>>>> tibble(dates = c(rep("2021-07-04", 2), rep("2021-07-25", 3),
>>>> rep("2021-07-18", 4)))
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>