[R] Assigning categorical values to dates

N. F. Parsons n@th@n@|@p@r@on@ @end|ng |rom gm@||@com
Thu Jul 22 22:15:57 CEST 2021


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 <ericjberger using gmail.com> wrote:

> 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 and Rui went with the spirit and
> added an anonymous function using the new syntax.
>
> Everyone has their preferred coding style. I tend to prefer fewer lines of
> code (if there is no cost in understanding).
> I think the new anonymous function syntax helps in this regard and I see
> no reason to use piping if not necessary.
> So here is a modified, one-line version of Rui's last suggestion (sans the
> amazing observation about handling interactions).
>
> mutate(date_df, cycle=(\(ranks) match(dates,
> ranks))(sort(unique(dates))))
>
> Eric
>
>
>
>
> On Thu, Jul 22, 2021 at 11:11 AM Uwe Ligges <
> ligges using statistik.tu-dortmund.de> wrote:
>
>> 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 using 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.
>> >
>>
>> ______________________________________________
>> R-help using 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.
>>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list