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 dates cycle <chr> <chr> 2021-07-04 1 2021-07-04 1 2021-07-25 3 2021-07-25 3 2021-07-25 3 2021-07-18 2 2021-07-18 2 2021-07-18 2 2021-07-18 2 Not to further complicate matters, but some months I may only have one date, and some months I will have 4 dates - so thats not a fixed quantity. We've literally been doing this by hand at my job and I'd like to automate it. Thanks in advance! Nate Parsons [[alternative HTML version deleted]]
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. Quoting "N. F. Parsons" <nathan.f.parsons at gmail.com>:> 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 > > dates cycle > <chr> <chr> > 2021-07-04 1 > 2021-07-04 1 > 2021-07-25 3 > 2021-07-25 3 > 2021-07-25 3 > 2021-07-18 2 > 2021-07-18 2 > 2021-07-18 2 > 2021-07-18 2 > > Not to further complicate matters, but some months I may only have one > date, and some months I will have 4 dates - so thats not a fixed quantity. > We've literally been doing this by hand at my job and I'd like to automate > it. > > Thanks in advance! > > Nate Parsons > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
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))) > > how in the world do I add a column that evaluates each of those dates and > assigns it a categorical value such that > > dates cycle > <chr> <chr> > 2021-07-04 1 > 2021-07-04 1 > 2021-07-25 3 > 2021-07-25 3 > 2021-07-25 3 > 2021-07-18 2 > 2021-07-18 2 > 2021-07-18 2 > 2021-07-18 2 > > Not to further complicate matters, but some months I may only have one > date, and some months I will have 4 dates - so thats not a fixed quantity. > We've literally been doing this by hand at my job and I'd like to automate > it. > > Thanks in advance! > > Nate Parsons > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >