Hello, I have been surprised when converting a character string to a date with the following format, in R 4.1.0 (linux debian 10) as.Date("20-12-2020","%Y-%m-%d") [1] "20-12-20" in R 4.0.5 (window 10) as.Date("20-12-2020","%Y-%m-%d") [1] "0020-12-20" Here I was expecting a blunt and sharp NA, am I missing something? Best regards, Jeremie
On 01.07.2021 10:06, Jeremie Juste wrote:> Hello, > > I have been surprised when converting a character string to a date with the following > format, > > in R 4.1.0 (linux debian 10) > > as.Date("20-12-2020","%Y-%m-%d") > [1] "20-12-20" > > in R 4.0.5 (window 10) > > as.Date("20-12-2020","%Y-%m-%d") > [1] "0020-12-20"Yes, it is rather strange to specify "2020" as the day and "20" as the 4digits year, so different implementations may print the year in 2 or 4 digits. What you want is actually as.Date("20-12-2020","%d-%m-%Y") Best, Uwe Ligges> > > Here I was expecting a blunt and sharp NA, am I missing something? > > Best regards, > Jeremie > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi Jeremie, Try: as.Date("20-12-2020","%y-%m-%d") [1] "2020-12-20" Jim On Thu, Jul 1, 2021 at 6:16 PM Jeremie Juste <jeremiejuste at gmail.com> wrote:> > Hello, > > I have been surprised when converting a character string to a date with the following > format, > > in R 4.1.0 (linux debian 10) > > as.Date("20-12-2020","%Y-%m-%d") > [1] "20-12-20" > > in R 4.0.5 (window 10) > > as.Date("20-12-2020","%Y-%m-%d") > [1] "0020-12-20" > > > Here I was expecting a blunt and sharp NA, am I missing something? > > Best regards, > Jeremie > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hm. Seems to me, that both your codes are wrong but printing in Linux is different from Windows. With as.Date("20-12-2020","%Y-%m-%d") you say that 20 is year (actually year 20) and 2020 is day and only first two values are taken (but with some valueas result is NA) I can confirm 4.0.3 in Windows behaves this way too.> as.Date("20-12-2020","%Y-%m-%d")[1] "0020-12-20" Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Jeremie Juste > Sent: Thursday, July 1, 2021 10:06 AM > To: r-help <r-help at r-project.org> > Subject: [R] Unexpected date format coercion > > Hello, > > I have been surprised when converting a character string to a date withthe> following format, > > in R 4.1.0 (linux debian 10) > > as.Date("20-12-2020","%Y-%m-%d") > [1] "20-12-20" > > in R 4.0.5 (window 10) > > as.Date("20-12-2020","%Y-%m-%d") > [1] "0020-12-20" > > > Here I was expecting a blunt and sharp NA, am I missing something? > > Best regards, > Jeremie > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.