Efficient and accurate age calculation (in years, months, or weeks) in R given birth date and an arbitrary date

后端 未结 4 867
难免孤独
难免孤独 2020-11-30 06:28

I am facing the common task of calculating the age (in years, months, or weeks) given the date of birth and an arbitrary date. The thing is that quite often I have to do thi

4条回答
  •  生来不讨喜
    2020-11-30 07:29

    The reason lubridate appears to be making mistakes above is that you are calculating duration (the exact amount of time that occurs between two instants, where 1 year = 31536000s), rather than periods (the change in clock time that occurs between two instants).

    To get the change in clock time (in years, months, days, etc) you need to use

    as.period(interval(start = birthdate, end = givendate))
    

    which gives the following output

     "37y 0m 1d 0H 0M 0S"   
     "37y 0m 0d 0H 0M 0S"   
     "36y 11m 30d 0H 0M 0S" 
     ...
     "46y 11m 30d 1H 0M 0S" 
     "47y 0m 0d 1H 0M 0S"   
     "47y 0m 1d 1H 0M 0S" 
    

    To just extract years, you can use the following

    as.period(interval(start = birthdate, end = givendate))$year
     [1] 37 37 36 53 53 52 50 50 49  1  1  0 46 47 47
    

    Note sadly appears even slower than the methods above!

    > mbm
    Unit: microseconds
           expr       min        lq       mean    median         uq        max neval cld
     arithmetic   116.595   138.149   181.7547   184.335   196.8565   5556.306  1000  a 
      lubridate 16807.683 17406.255 20388.1410 18053.274 21378.8875 157965.935  1000   b
    

提交回复
热议问题