Jeni
Jeni

Reputation: 958

Calculate the difference between to date columns of a dataframe

How can I get the difference between Date1 and Date2 columns of my dataframe?

   Date1        Tfd             Date2       Sex
 13/08/1936      3           09/01/2013      M     
 25/04/1948      2           14/05/2014      M
 26/01/1939      1           03/07/2015      F
 13/02/1935      8           03/08/2012      F

I have tryed:

age<-apply(df[, c("Date1", "Date2")], function(x, y) difftime(strptime(y, format = "%d.%m.%Y"), strptime(x, format = "%d.%m.%Y"),units="years"))

but I get this error:

Error in strptime(y, format = "%d.%m.%Y") : 
  argument "y" is missing, with no default

Do you know how can I solve this?

Upvotes: 0

Views: 37

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

You don't need apply here :

as.numeric(as.Date(df$Date2, "%d/%m/%Y") - as.Date(df$Date1, "%d/%m/%Y"))
#[1] 27908 24125 27917 28296

difftime does not have units as 'years'. The maximum units it has is of weeks. You can divide the week value with 52.25 to get year of use lubridate's time_length function.

Or using dplyr with difftime

library(dplyr)
library(lubridate)

df %>%
  mutate_at(vars(starts_with('date')), lubridate::dmy) %>%
  mutate(diff = time_length(difftime(Date2, Date1), 'years'))

#       Date1 Tfd      Date2 Sex diff
#1 1936-08-13   3 2013-01-09   M 76.4
#2 1948-04-25   2 2014-05-14   M 66.1
#3 1939-01-26   1 2015-07-03   F 76.4
#4 1935-02-13   8 2012-08-03   F 77.5

Upvotes: 1

Related Questions