Re: [R] Assigning categorical values to dates

2021-07-22 Thread N. F. Parsons
Thank you all so much for your time and your help! I am truly grateful for the suggested solutions, but more importantly, for the lessons! Nate Parsons On Thu, Jul 22, 2021 at 4:13 AM Eric Berger wrote: > While the base R solution using 'factor' appears to win based on elegance, > chapeau to

Re: [R] Assigning categorical values to dates

2021-07-22 Thread Eric Berger
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

Re: [R] Assigning categorical values to dates

2021-07-22 Thread Uwe Ligges
For a data.frame d, I'd simply do d$cycle <- factor(d$dates, labels=1:3) but I have not idea about tibbles. Best, Uwe Ligges On 22.07.2021 05:12, N. F. Parsons wrote: Hi all, If I have a tibble as follows: tibble(dates = c(rep("2021-07-04", 2), rep("2021-07-25", 3), rep("2021-07-18",

Re: [R] Assigning categorical values to dates

2021-07-22 Thread Rui Barradas
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

Re: [R] Assigning categorical values to dates

2021-07-22 Thread N. F. Parsons
I had no idea that ‘cur_group_id()’ existed!?!! Will definitely try that. Thank you!!! — Nathan Parsons, B.SC, M.Sc, G.C. Ph.D. Candidate, Dept. of Sociology, Portland State University Adjunct Professor, Dept. of Sociology, Washington State University Graduate Advocate, American Association of

Re: [R] Assigning categorical values to dates

2021-07-22 Thread Erich Subscriptions
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

Re: [R] Assigning categorical values to dates

2021-07-22 Thread Rui Barradas
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)))