0

I have dataframe and there two columns of 1st_date date and 2nd_date. I used difftime() function to calculate the difference by days, and it did the job. However, I found an incorrect value when I checked all values generated by difftime()

The incorrect value I have is:

    1st_date        2nd_date      time_diff
  12/31/68 12:11   1/13/69 19:29    -36512

while it should be:

     1st_date         2nd_date      time_diff
  12/31/68 12:11    1/13/69 19:29      13

the command I used is :

df$time_diff <- difftime(as.Date(df$1st_date, format="%m/%d/%y"),
                as.Date(df$2nd_date, format="%m/%d/%y"), 
                units = "days")

Any one can explain how did that happen and how to handle it?

  • 3
    Possible duplicate of [Converting dates before January 1, 1970 in R](http://stackoverflow.com/questions/38508886/converting-dates-before-january-1-1970-in-r) – nrussell Apr 02 '17 at 18:46
  • 1
    try `difftime(as.Date(format(as.Date("12/31/68 12:11", format="%m/%d/%y"), "19%y%m%d"), "%Y%m%d"), as.Date(format(as.Date("1/13/69 19:29", format="%m/%d/%y"), "19%y%m%d"), "%Y%m%d"), units = "days")` – Mislav Apr 02 '17 at 19:03
  • it works perfectly. Thanks! – Ali Algarni Apr 02 '17 at 19:30

1 Answers1

0

Solution:

difftime(as.Date(format(as.Date("12/31/68 12:11", format="%m/%d/%y"), "19%y%m%d"), "%Y%m%d"), as.Date(format(as.Date("1/13/69 19:29", format="%m/%d/%y"), "19%y%m%d"), "%Y%m%d"), units = "days")
Mislav
  • 1,533
  • 16
  • 37