3
as.numeric(difftime(
strptime("12:00 AM", "%I:%M %p" ),
strptime("2:00 AM", "%I:%M %p" ),
 units='hours'))

result: NA.
I am trying to calculate the time difference, but some rows generate NA values which are really annoying, something wrong here?

DatascienceGeeks
  • 368
  • 2
  • 12
  • 1
    @akrun to replicate, see my answer. Given the behavior, OP is likely in a locale that is in the middle of a time change in that period (last Sunday in March). See https://en.wikipedia.org/wiki/Daylight_saving_time_by_country – De Novo Mar 25 '18 at 16:24

1 Answers1

2

To replicate, do:

as.numeric(difftime(
 strptime("12:00 AM", "%I:%M %p", tz = "Europe/Brussels" ),
 strptime("2:00 AM", "%I:%M %p", tz = "Europe/Brussels" ),
 units='hours'))
# [1] NA

Given the behavior you're getting, I expect you are running into a daylight savings time issue because you are in a locale that has a daylight saving time change on the last Sunday of March. There is no "roll" argument for strptime or difftime so you have to set the time zone manually to a location that isn't in the middle of a time change in that period (and if you want to account for it, add an hour).

as.numeric(difftime(
 strptime("12:00 AM", "%I:%M %p", tz = "UTC" ),
 strptime("2:00 AM", "%I:%M %p", tz = "UTC" ),
 units='hours'))
# [1] -2
De Novo
  • 7,120
  • 1
  • 23
  • 39