The command "% in%" versus a regular subset in r

I have a simple question. Why r returns 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
+4
source share
1 answer

%in%does not recognize the shape of the characters 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"),]

Although it is obvious that in this case it ==works fine, since you are matching only one value.

+6
source

All Articles