In this tutorial we will learn how to find the difference between two date objects in R in terms of days, months, and years. We will learn how to use time_length() function available in tidyverse as part of lubridate package to get the difference as dyas, weeks, months, and years.
1 2 3 4 5 6 7 8 9 10 11 12 | library (tidyverse) ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ── <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > dplyr 1.1.4 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > readr 2.1.5 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > forcats 1.0.0 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > stringr 1.5.1 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > ggplot2 3.5.1 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > tibble 3.2.1 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > lubridate 1.9.3 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > tidyr 1.3.1 <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2714.svg" > purrr 1.0.2 ── Conflicts ────────────────────────────────────────── tidyverse_conflicts () ── <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2716.svg" > dplyr:: filter () masks stats:: filter () <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2716.svg" > dplyr:: lag () masks stats:: lag () <img draggable= "false" role= "img" class= "emoji" alt= " src= "https://s.w.org/images/core/emoji/15.0.3/svg/2139.svg" > Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to beco |
Let us create two date variables using as.Date() function.
1 2 | date1 <- as.Date ( "2014-01-01" ) date2 <- as.Date ( "2015-12-31" ) |
And here is how our date variable looks like.
1 2 3 | date1 [1] "2014-01-01" |
1 2 3 | date2 [1] "2015-12-31" |
Difference between two dates in days
We can find the difference between two dates in terms of the number of days, by using subtraction. In the example below date1 precedes date2, therefore we get the number of days as negative.
1 2 3 | date1 - date2 Time difference of -729 days |
By subtracting date1 from date2, we will get the difference in terms days as we would like.
1 2 3 | date2 - date1 Time difference of 729 days |
We can also use the function time_length() function with unit=”days” as argument to get the number of days between two dates.
1 2 3 | time_length (date2-date1, unit= "days" ) [1] 729 |
Difference between two dates in years
With time_length() function using unit=”years” as argument we will get the number of years between two dates. Note that the result we get is a float and we can round it to get the number of years as integer.
1 2 3 | time_length (date2-date1, unit= "years" ) [1] 1.995893 |
Difference between two dates in months
Using unit=”months” as argument to time_length() function as shown below we get the number of months between the two dates as a float.
1 2 3 | time_length (date2-date1, unit= "months" ) [1] 23.95072 |
Difference between two dates in weeks
Similarly, using unit=”weeks” as argument to time_length() function, we get the time difference between two dates in weeks.
1 2 3 | time_length (date2-date1, unit= "weeks" ) [1] 104.1429 |