lundi 6 avril 2020

Using date to create a sequence of numbers

R / Stackoverflow newbie here.

I have a dataset that looks like:

    year mother_id   nest_seq incubation_date
   <int> <chr>          <int> <date>         
 1  1994 543762-MMM         1 1994-10-16     
 2  1994 543762-MMM         3 1994-11-06     
 3  1994 543762-MMM         4 1994-11-24     
 4  1994 543762-MMM         6 1994-12-05     
 5  1995 583809-mGMW        4 1994-10-24     
 6  1995 583809-mGMW        7 1994-11-21     
 7  1995 583809-mGMW        8 1994-12-22     
 8  1996 596751-BWM         1 1994-11-20     
 9  1996 596751-BWM         2 1994-12-23     
10  1996 626691-GBW         2 1994-11-08

I simply want to generate a new nest_seq based on the incubation_date For example:

 year mother_id   nest_seq incubation_date new_nest_seq
   <int> <chr>          <int> <date>         <int>
 1  1994 543762-MMM         1 1994-10-16        1
 2  1994 543762-MMM         3 1994-11-06        2
 3  1994 543762-MMM         4 1994-11-24        3
 4  1994 543762-MMM         6 1994-12-05        4

I have been trying to do this using if_else() but getting stuck...

group_by(year, mother_id) %>%
mutate(new_nest_seq = if_else(min(incubation_date), 1, ?)))

Would greatly appreciate any advice...

Aucun commentaire:

Enregistrer un commentaire