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 t

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 pipe

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", 4)

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 U

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 05

Re: [R] Assigning categorical values to dates

2021-07-21 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)))

Re: [R] Assigning categorical values to dates

2021-07-21 Thread Bert Gunter
You have been told how to do it. If you do not understand, you should find a suitable tutorial to learn about how R factors work. There are some difficulties in converting dates on an ongoing basis to factors, so I think you should take Tom's advice to rethink this. It sounds as if you might also d

Re: [R] Assigning categorical values to dates

2021-07-21 Thread Tom Woolman
Not if you use as.factor to convert a character type column to factor levels. It should recode the distinct string values to factors automatically for you. i.e., df$datefactors <- as.factor(df$datestrings) Quoting "N. F. Parsons" : I am not averse to a factor-based solution, but I would

Re: [R] Assigning categorical values to dates

2021-07-21 Thread N. F. Parsons
I am not averse to a factor-based solution, but I would still have to manually enter that factor each month, correct? If possible, I’d just like to point R at that column and have it do the work. — Nathan Parsons, B.SC, M.Sc, G.C. Ph.D. Candidate, Dept. of Sociology, Portland State University A

Re: [R] Assigning categorical values to dates

2021-07-21 Thread Tom Woolman
Couldn't you convert the date columns to character type data in a data frame, and then convert those strings to factors in a 2nd step? The only downside I think to treating dates as factor levels is that you might have an awful lot of factors if you have a large enough dataset. Quoti

[R] Assigning categorical values to dates

2021-07-21 Thread N. F. Parsons
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", 4))) how in the world do I add a column that evaluates each of those dates and assigns it a categorical value such that datescycle 2021-07-04