% in% vs '==' when comparing date with date_as_string

I am confused why %in%and '=='give different results here:

day_string <- '2017-07-20'
day_date <- as.Date(day_string)

day_string == day_date #TRUE
day_string %in% day_date #FALSE

From the reference %in%: %in%currently defined as"%in%" <- function(x, table) match(x, table, nomatch = 0) > 0

So, if I understand things correctly, as it matchforces the date to a character (but first to a numeric one),

day_string %in% day_date 

translates to

match(day_string, as.character(as.numeric(day_date)), nomatch = 0) > 0

However, '=='help says that it also forces different types. What does '=='the above example really do and why does it behave differently than %in%?

+4
source share
2 answers

"==" ( ?"==") , / , ("==.class"), Ops (Ops.class). , R, "", , "==" ?Ops.Date. , "" methods(class = "Date").

, match ( "%in%") "" ( R ). , , , , . ( ), , "POSIXlt" (day_string %in% as.POSIXlt(day_date) ). , "%in%" "day_date", , , , a typeof(day_date) (unclass(day_date)) typeof(day_string), (, - as.character.default(day_date)) ?match.

+1

?== " , " , == , %in% .

Vs,

as.character(5) %in% 5
#[1] TRUE

as.factor('abc') %in% 'abc'
#[1] TRUE

5 %in% 5L
#[1] TRUE

OP, @Cath, df_date , , ,

as.character(as.numeric(day_date))
#[1] "17367"

as.character(as.numeric(day_date)) %in% day_string
#[1] FALSE

,

'17367' %in% as.Date(day_string)
#[1] TRUE
+3

Source: https://habr.com/ru/post/1681853/


All Articles