Max and min date other than title, tail of sorted list

I found some bewilderment. Given a list of 50 dates:

structure(c("15513", "12830", "16503", "-3628", "15833", "13553", 
"473", "17126", "7916", "74", "4736", "7712", "12726", "8684", 
"16217", "14725", "11836", "7061", "4687", "8733", "17176", "17175", 
"16155", "14005", "14635", "-1793", "4296", "15316", "4746", 
"6865", "14228", "5177", "4543", "1936", "10372", "-1393", "13648", 
"17267", "9177", "10380", "9427", "9527", "-1375", "2133", "13966", 
"336", "1925", "9611", "15601", "9218"), class = "Date")

I ran this:

head(sort(b), 1) ## prints "1960-01-26"
tail(sort(b), 1) ## prints "2017-04-11"

max(b)           ## prints "1996-04-25"
min(b)           ## prints "1966-03-28"

But it really bothers me when all the printed values ​​are in the same list maxand minreturn the expected result.

dc = as.Date(c("1960-01-26", "2017-04-11", "1996-04-25", "1966-03-28"))
max(dc)    ## prints "2017-04-11"
min(dc)    ## prints "1960-01-26"

Why is this?

+4
source share
1 answer

The functions min () and max () are intended only for entering the class "numeric", the Vector in your example, I class "Date". For:

structure(min(as.numeric(example)), class ="Date")

where "example" is your vector, you get the right solution ...

Hope this makes it clear.

Greetings

0
source

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


All Articles