-2

I have the following dataframe (DF1):

Date             Value   
29/12/2014 8:00  24.940   
29/12/2014 9:00  24.960   
29/12/2014 11:00 25.020 

I would like to add a new column for DF1$DIFF, where it contains the difference in values between each line's Date (including hours) to its above Date. So that the required results will be:

Date             Value   Diff
29/12/2014 8:00  24.940   
29/12/2014 9:00  24.960    1
29/12/2014 11:00 25.020    2

I tried to use the as.date function, however, I get the difference in dates only:

> as.Date("2009-10-01 10:00")- as.Date("2009-10-01 9:00")
Time difference of 0 days
Avi
  • 2,247
  • 4
  • 30
  • 52
  • 1
    Try searching "r difference hours" in google or rseek. First result is `difftime` – RHA Jan 10 '16 at 08:47
  • Thanks a lot @RHA, It works great but my date format is a bit different. Here is the result: > difftime("2009-10-01 11:00", "2009-10-01 9:00", units="hours") Time difference of 2 hours > difftime("29/12/2014 11:00", "29/12/2014 9:00", units="hours") Time difference of 0 hours – Avi Jan 10 '16 at 08:53

2 Answers2

3

Initially, you can't use as.Date if you want to look at differences in hours, as that will discard the hour information. Instead use as.POSIXct. You can then calculate the difference doing:

as.POSIXct(strptime("2009-10-01 10:00:00", "%Y-%m-%d %H:%M:%S")) -
as.POSIXct(strptime("2009-10-01 09:00:00", "%Y-%m-%d %H:%M:%S"))

Giving you Time difference of 1 hours.

JCollerton
  • 3,227
  • 2
  • 20
  • 25
  • 1
    Why would you need `strptime` here? Isn't just `as.POSIXct("2009-10-01 10:00:00") - as.POSIXct("2009-10-01 09:00:00")` is enough? – David Arenburg Jan 10 '16 at 09:04
  • Thanks a lot JCollerton, please note that my format is a bit different here is the solution: as.POSIXct(strptime("29/12/2014 10:00:00", "%d/%m/%Y %H:%M:%S")) - as.POSIXct(strptime("29/12/2014 08:00:00", "%d/%m/%Y %H:%M:%S")) – Avi Jan 10 '16 at 09:05
  • And please for the completeness add a new column for the dataframe: DF1$Diff <- ... – Avi Jan 10 '16 at 09:08
  • @DavidArenburg If you don't use `strptime` first, I get a lot of warnings about timezones. I think by using it you can get rid of these warnings, but keep the warnings to do with ambiguous times (for example if the clock goes forward an hour and you enter a time within that hour). @Avi, glad it helps! – JCollerton Jan 10 '16 at 09:13
  • I don't get any warnings. But regardless, specifying time zone is always the best practice in order to avoid unexpected results from both functions. – David Arenburg Jan 10 '16 at 09:15
2

Try using the package lubridate. Here is a way you can do this using lubridate

library(lubridate)

difftime(ymd_hm("2009-10-01 10:00"), ymd_hm("2009-10-01 9:00"))
#Time difference of 1 hours

or

ymd_hm("2009-10-01 10:00") - ymd_hm("2009-10-01 9:00")
#Time difference of 1 hours

Another example

difftime(dmy_hm("29/12/2014 11:00"), dmy_hm("29/12/2014 9:00"), units="hours")
#Time difference of 2 hours

or

ymd_hm("2009-10-01 10:00") - ymd_hm("2009-10-01 9:00")
#Time difference of 1 hours
steveb
  • 5,382
  • 2
  • 27
  • 36