Home > OS >  How to calculate the persondays in a year based on two date columns
How to calculate the persondays in a year based on two date columns

Time:04-14

I would like to count the amount of days an ID/row(person) has during a period of 1 year. Below an example of my dataset. (Inschrijfdatum = registration date, Uitschrijfdatum = deregistration date)

   Inschrijfdatum Uitschrijfdatum
   <date>         <date>         
 1 1996-02-22     2019-01-11        
 2 2011-10-31     2019-02-25         
 3 1992-06-15     2019-03-10         
 4 2001-11-13     2022-01-01             
 5 2019-02-18     2019-09-07        
 6 2019-12-30     2022-01-01           
# ... with 12 more rows

df <-structure(list(Inschrijfdatum = structure(c(9548, 15278, 8201, 
11639, 10029, 15134, 17756, 16526, 17945, 13427, 7348, 16072, 
13195, 8231, 12620, 14767, 17893, 18054, 15300, 12006, 15399, 
10092), class = "Date"), Uitschrijfdatum = structure(c(17907, 
17952, 17965, 17981, 18001, 18079, 18102, 18137, 18146, 18190, 
18198, 18199, 18222, 18233, 18240, 18487, 18496, 18542, 18555, 
18581, 18670, 18993), class = "Date")), row.names = c(NA, -22L
), class = c("tbl_df", "tbl", "data.frame"))

I would like to create a new column in which the total 'person'days are included in a certain year (in this case year 2019)

   Inschrijfdatum Uitschrijfdatum    persondays
   <date>         <date>         
 1 1996-02-22     2019-01-11         11
 2 2011-10-31     2019-02-25         56 
 3 1992-06-15     2019-03-10         69 
 4 2001-11-13     2022-01-01         365     
 5 2019-02-18     2019-09-07         200
 6 2019-12-30     2022-01-01         1

I've tried doing it in steps:

  1. df$persondays <- df$Uitschrijfdatum - as.Date("2019-01-01", format= "%Y-%m-%d"))
  2. df$persondays <- as.Date("2020-01-01", format= "%Y-%m-%d")) - df$Inschrijfdatum

This offcourse doesnt work quite right. I would like to create a script that can count it for each of the following possibilities

       2019                      2020                1=inschrijfdatum, 2= uitschrijfdatum
       |--------------------------|
1-------------2                                    ? (2 - as.date(2019)) 
1---------------------------------------2          ? maximum of 365 days 
             1-----------2                         ? days between 1 and 2
                              1---------2          ? (as.date(2020) - 1)

I cant quite figure this out. Any help would be much appreciated!

Many thanks in advance!

CodePudding user response:

Perhaps like so?

df$start = pmax(df$Inschrijfdatum, as.Date("2019-01-01"))
df$end   = pmin(df$Uitschrijfdatum, as.Date("2020-01-01"))
df$persondays = as.numeric(df$end - df$start   1)

Result

# A tibble: 22 × 5
   Inschrijfdatum Uitschrijfdatum start      end        persondays
   <date>         <date>          <date>     <date>          <dbl>
 1 1996-02-22     2019-01-11      2019-01-01 2019-01-11         11
 2 2011-10-31     2019-02-25      2019-01-01 2019-02-25         56
 3 1992-06-15     2019-03-10      2019-01-01 2019-03-10         69
 4 2001-11-13     2019-03-26      2019-01-01 2019-03-26         85
 5 1997-06-17     2019-04-15      2019-01-01 2019-04-15        105
 6 2011-06-09     2019-07-02      2019-01-01 2019-07-02        183
 7 2018-08-13     2019-07-25      2019-01-01 2019-07-25        206
 8 2015-04-01     2019-08-29      2019-01-01 2019-08-29        241
 9 2019-02-18     2019-09-07      2019-02-18 2019-09-07        202
10 2006-10-06     2019-10-21      2019-01-01 2019-10-21        294
# … with 12 more rows
  • Related