0

I have troubles to understand why R returns FALSE if I check whether a date-time element is contained in a vector with %in% although the date time element I check for is definitely contained in the vector. Put differently, I wonder how these outputs are possible:

> GOLD.5.event.times[1] == GOLD.5$`1.Date & Time`[24544]
[1] TRUE
> GOLD.5.event.times[1] %in% GOLD.5$`1.Date & Time`
[1] FALSE

I would expect a TRUE return from the %in% operation as the respective date time value is contained in the vector.

A quick overview over the type of data I use:

> GOLD.5.event.times <- seq(ISOdatetime(2017, 4, 4, 0, 0, 0, tz = ""),
ISOdatetime(2017, 4, 5, 0, 0, 0, tz = ""), "5 min")
> GOLD.5.event.times[1:10]
[1] "2017-04-04 00:00:00 CEST" "2017-04-04 00:05:00 CEST" "2017-04-04  
00:10:00 CEST" "2017-04-04 00:15:00 CEST" "2017-04-04 00:20:00 CEST"
[6] "2017-04-04 00:25:00 CEST" "2017-04-04 00:30:00 CEST" "2017-04-04
00:35:00 CEST" "2017-04-04 00:40:00 CEST" "2017-04-04 00:45:00 CEST"

> GOLD.5$`1.Date & Time`[24540:24550] # result of strptime() conversion
[1] "2017-04-03 22:55:00 CEST" "2017-04-03 23:00:00 CEST" "2017-04-03
23:45:00 CEST" "2017-04-03 23:55:00 CEST" "2017-04-04 00:00:00 CEST"
[6] "2017-04-04 00:05:00 CEST" "2017-04-04 00:10:00 CEST" "2017-04-04
00:15:00 CEST" "2017-04-04 00:20:00 CEST" "2017-04-04 00:25:00 CEST"
[11] "2017-04-04 00:30:00 CEST"

And the dputs.

> dput(GOLD.5$`1.Date & Time`[24544])
structure(list(sec = 0, min = 0L, hour = 0L, mday = 4L, mon = 3L, 
year = 117L, wday = 2L, yday = 93L, isdst = 1L, zone = "CEST", 
gmtoff = NA_integer_), .Names = c("sec", "min", "hour", "mday", 
"mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"), class =
c("POSIXlt", "POSIXt"))
> dput(GOLD.5.event.times[1])
structure(1491256800, class = c("POSIXct", "POSIXt"), tzone = "")
apitsch
  • 1,532
  • 14
  • 31
  • 2
    Add a `dput` of your data – Hong Ooi Jun 07 '17 at 11:29
  • 2
    Please provide a reproducible example including example data, the code you have tried and the expected result. – Pierre Lapointe Jun 07 '17 at 11:30
  • make sure your dates are formatted correctly. I couldn't recreate the problem. For instance what does `str(Gold.5.event.times)` give you? and what does `str(Gold.5$'1.Date & Time')` give? – Chris Jun 07 '17 at 11:52
  • 2
    Thanks for providing the dputs. Using `str()` on each shows that your first dataframe does not have the time element attached, so no match will be possible. – Chris Jun 07 '17 at 11:58
  • 1
    I get FALSE for `==` on these as well as `%in%`, because they are not the same. One has a time and a time zone, the other does not, and one is POSIXlt and the other POSIXct. – Rich Scriven Jun 07 '17 at 12:00
  • Where you have `tz=""`, change to `tz="CET"` to make it compatable with your data – Chris Jun 07 '17 at 12:01
  • 1
    Thanks for all your comments. @HongOoi and Chris you have put me on the right lead. Different classes were the problem. I changed the strptime() conversion to as.POSIXct() and it worked fine as well. For anyone else experiencing similar problems this [link](https://stackoverflow.com/questions/10699511/difference-between-as-posixct-as-posixlt-and-strptime-for-converting-character-v#10700025) might help. – apitsch Jun 07 '17 at 12:08

1 Answers1

2

Solved in comments.

Different classes of datetime elements caused the problem. Check this for an overview of datetime conversion functions.

apitsch
  • 1,532
  • 14
  • 31