4

I have a simple question. Why does r return different results for

dim(data2[data2$as_of_date=="2014-12-31",])
dim(data2[data2$as_of_date%in%c("2014-12-31"),])

?

output:

> dim(data2[data2$as_of_date=="2014-12-31",])
[1] 48684    92
> dim(data2[data2$as_of_date%in%c("2014-12-31"),])
[1]  0 92
Paul
  • 1,106
  • 1
  • 16
  • 39
  • Maybe a duplicate: http://stackoverflow.com/questions/15358006/difference-between-in-vs but not sure enough to vote to close. – Tyler Rinker Feb 05 '15 at 19:37
  • 1
    Perhaps, see `today = Sys.Date(); Sys.Date() == as.character(today); Sys.Date() %in% as.character(today)` – alexis_laz Feb 05 '15 at 19:38
  • Thank you both. Actually I just realized the answer. It is a different problem from the linked post. Closer to what alexis_laz suggested. It had to do with the fact that "==" automatically typecasts a string to a date if it is being compared to a date. But "%in%" does not do this -- so since data2$as_of_date is a date and "2014-12-31" is a string, it returns no matches, unlike "==" – Paul Feb 05 '15 at 19:40
  • 1
    @TylerRinker: not a duplicate. The one you link to hangs on vector recycling, which is not an issue here. – Stephan Kolassa Feb 05 '15 at 19:40
  • 1
    Just to add something that I found out: `match` uses [match_transform](http://svn.r-project.org/R/trunk/src/main/unique.c) which converts "factor"s and "POSIXlt"s (not "Date"s) to "character". E.g. `Sys.Date() %in% as.character(today)` VS `as.POSIXlt(Sys.Date()) %in% as.character(today)` – alexis_laz Feb 05 '15 at 19:53
  • @StephanKolassa Thanks. Glad I didn't vote to close. – Tyler Rinker Feb 05 '15 at 20:59

1 Answers1

6

%in% doesn't recognize the "character" form of your dates. Consider:

> as.Date("2014-12-31") == "2014-12-31"
[1] TRUE
> as.Date("2014-12-31") %in% "2014-12-31"
[1] FALSE

You need to use:

data2[as.character(data2$as_of_date) %in% c("2014-12-31"),]

Though obviously in this case == works fine since you're matching just one value.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • Thank you very much for your answer. Interestingly, casting both arguments to the comparator as a date did not work. i.e. `data2[as.Date(data2$as_of_date) %in% as.Date(c("2014-12-31")),]` still returned no matches. But casting them as characters worked perfectly – Paul Feb 05 '15 at 19:57
  • 3
    @Paul, odd, `as.Date("2014-12-31") %in% as.Date("2014-12-31")` returns TRUE. – BrodieG Feb 05 '15 at 19:59