Time Difference in Years with Lubridate

Time difference in years with lubridate?

This is the lubridate approach I would take:

interval(dob, today) / years(1)

Yields the answer of 32 years.

Note that the function will complain that it cannot express the remainder of the fraction of the year. This is because year is not a fixed concept, i.e. 366 in leap years and 365 in non-leap years. You can get an answer with more detail in regard to the number of weeks and days:

interval_period = interval(dob, today)
full_year = interval_period %/% years(1)
remaining_weeks = interval_period %% years(1) %/% weeks(1)
remaining_days = interval_period %% years(1) %% weeks(1) %/% days(1)
sprintf('Your age is %d years, %d weeks and %d days', full_year, remaining_weeks, remaining_days)
# [1] "Your age is 32 years, 51 weeks and 1 days"

Note that I use %/% for division and %% as modulo to get the remaining weeks/days after subtracting the full years/weeks.

Get date difference in years (floating point)

Yes, of course, use difftime() with an as numeric:

R> as.numeric(difftime(as.Date("2003-04-05"), as.Date("2001-01-01"), 
+ unit="weeks"))/52.25
[1] 2.2529
R>

Note that we do have to switch to weeks scaled by 52.25 as there is a bit of ambiguity
there in terms of counting years---a February 29 comes around every 4 years but not every 100th etc.

So you have to define that. difftime() handles all time units up to weeks. Months cannot be done for the same reason of the non-constant 'numerator'.

Get the difference between dates in terms of weeks, months, quarters, and years

what about this:

# get difference between dates `"01.12.2013"` and `"31.12.2013"`

# weeks
difftime(strptime("26.03.2014", format = "%d.%m.%Y"),
strptime("14.01.2013", format = "%d.%m.%Y"),units="weeks")
Time difference of 62.28571 weeks

# months
(as.yearmon(strptime("26.03.2014", format = "%d.%m.%Y"))-
as.yearmon(strptime("14.01.2013", format = "%d.%m.%Y")))*12
[1] 14

# quarters
(as.yearqtr(strptime("26.03.2014", format = "%d.%m.%Y"))-
as.yearqtr(strptime("14.01.2013", format = "%d.%m.%Y")))*4
[1] 4

# years
year(strptime("26.03.2014", format = "%d.%m.%Y"))-
year(strptime("14.01.2013", format = "%d.%m.%Y"))
[1] 1

as.yearmon() and as.yearqtr() are in package zoo. year() is in package lubridate.
What do you think?

Difference in days between two dates in R using dplyr and lubridate?

Convert the columns to Date class and use difftime

df1$Difference <-  with(df1, as.numeric(difftime(as.Date(DeliveryDate), 
as.Date(ExpectedDate), units = "days")))

Or using tidyverse

library(dplyr)
library(lubridate)
df1 %>%
mutate(Difference = as.numeric(difftime(ymd(DeliveryDate),
ymd(ExpectedDate), units = "days")))
DeliveryDate ExpectedDate Difference
1 2022-01-05 2022-01-07 -2

lubridate [R]: difference between a date and now() to be added to a date

The Idee with lubridate is to take care of all the transformation between intervals and dates for you so you don't need to think about it. This simple code does exactly that what you want.

library(lubridate)

my_date <-as.POSIXlt(paste0("2009-08-",1:10))
time_diff <- now() - max(my_date)
time_diff_short = time_diff - 2
my_date + time_diff_short

What I found was that you need my_date to be of the format POSIXlt

Calculating Time Difference between two columns

Firstly, this has nothing to do with lubridate.

Secondly, RStudio has let you down by screwing with the printing of the variable in the display window. If you enter CR_Date$hours in the command line window you will see it prints

#Time differences in secs
#[1] 0 36900

and head(CR_Date) gives:

#        pos1                pos2      hours
#1 2014-07-01 2014-07-01 00:00:00 0 secs
#2 2014-07-01 2014-07-01 10:15:00 36900 secs

Either of which would have tipped you off as to what it is displaying.

As @Victorp suggests, difftime is the way to resolve this:

CR_Date$hours <- with(CR_Date, difftime(pos2,pos1,units="hours") )
CR_Date

# pos1 pos2 hours
#1 2014-07-01 2014-07-01 00:00:00 0.00 hours
#2 2014-07-01 2014-07-01 10:15:00 10.25 hours


Related Topics



Leave a reply



Submit