Skip to content
Prev 388587 / 398530 Next

Assigning categorical values to dates

Hello,

Here are 3 solutions, one of them the coercion to factor one.
Since you are using tibbles, I assume you also want a dplyr solution.


library(dplyr)

df1 <- tibble(dates = c(rep("2021-07-04", 2),
                         rep("2021-07-25", 3),
                         rep("2021-07-18", 4)))

# base R
as.integer(factor(df1$dates))
match(df1$dates, unique(sort(df1$dates)))

# dplyr
df1 %>% group_by(dates) %>% mutate(cycle = cur_group_id())


My favorite is by far the 1st but that's a matter of opinion.


Hope this helps,

Rui Barradas


?s 04:46 de 22/07/21, N. F. Parsons escreveu: