0

I try to find the difference between two timestamps. The codeQ:

survey <- data.frame(date=c("07/2012","07/2012"),tx_start=c("01/2012","01/2012"))
survey$date_diff <- as.Date(as.character(survey$date), format="%m/%Y")-
    as.Date(as.character(survey$tx_start), format="%m/%Y")
survey

I expect to have in the new column the different but I take NA

The results:

> survey
     date tx_start date_diff
1 07/2012  01/2012   NA days
2 07/2012  01/2012   NA days

What should I change to replace as.Date for months or years?

Update based on comment of Gregor:

    > survey <- data.frame(date=c("07/2012","07/2012"),tx_start=c("01/2012","01/2012"))
> survey$date <- as.Date(paste0("01/", as.character(survey$date)), "%d/%m/%Y")
> survey$tx_start <- as.Date(paste0("01/", as.character(survey$tx_start)), "%d/%m/%Y")
> survey$date_diff <- as.Date(survey$date, format="%d/%m/%Y")-
+     as.Date(survey$tx_start, format="%d/%m/%Y")
> survey
        date   tx_start date_diff
1 2012-07-01 2012-01-01  182 days
2 2012-07-01 2012-01-01  182 days
PitterJe
  • 216
  • 2
  • 12
  • 1
    Your dates are missing the day, try `as.Date(paste0("01/", as.character(survey$date)), "%d/%m/%Y")` and the same for `tx_start`. – Rui Barradas Dec 11 '17 at 18:47
  • Ok, but since it is not, paste the day, it won't hurt, you will not change the original data. – Rui Barradas Dec 11 '17 at 18:51
  • @PitterJe, Rui is correct. You cannot have a date difference without the date. So paste in an arbitrary but consistent date to get the date difference. – Adam Warner Dec 11 '17 at 19:00
  • @RuiBarradas thank. I tried and edited the question but again the problem exist. – PitterJe Dec 11 '17 at 19:21
  • Since neither `date` nor `tx_start` has days, you will need to use the `paste` trick with both (and consistent date formats). However, once something has been cast with `as.Date`, you don't need to `as.Date(as.character())` it anymore - you've already done it! – Gregor Thomas Dec 11 '17 at 19:25
  • @Gregor thank you I update the code based on your instructions and it works – PitterJe Dec 11 '17 at 20:29
  • 1
    As Nate said, check out the answers [at this question](https://stackoverflow.com/q/1995933/903061). There's a lot of good stuff there, especially Dirk's and pbnelson's answers. – Gregor Thomas Dec 11 '17 at 21:22

1 Answers1

1

I usually convert my dates to POSIXct format. Then, when direct differences are taken with normal syntax, you get an answer in units of seconds. There is a difftime() function in base R that you can use as well:

survey <- data.frame(date=c("07/2012","07/2012"),tx_start=c("01/2012","01/2012"))

# Dates are finicky, add a day so that conversion will work
survey$date2 <- paste0("01/",survey$date)
survey$tx_start2 <- paste0("01/",survey$tx_start)

# conversion
survey$date2 <-  as.POSIXct(x=survey$date2,format="%d/%m/%Y")
survey$tx_start2 <-  as.POSIXct(x=survey$tx_start2,format="%d/%m/%Y")

# take the difference
survey$date_diff <- with(survey,difftime(time1=date2,time2=tx_start2,units="hours"))
Nate
  • 364
  • 1
  • 5
  • 2
    There isn't, apparently, but you aren't the first to ask. [Here's one solution](https://stackoverflow.com/a/1995984/8469286). There may be something better out there, but I'm not aware of it. – Nate Dec 11 '17 at 19:33
  • 1
    @PitterJe look at the package called "lubridate" for difference in months – Adam Warner Dec 11 '17 at 19:37