Skip to content
Back to formatted view

Raw Message

Message-ID: <b6de96d4-2eac-46b6-9d92-fcf5a3f5646e@sapo.pt>
Date: 2024-07-28T11:50:39Z
From: Rui Barradas
Subject: help on date objects...
In-Reply-To: <PU4P216MB15684FD087C687AFD9055640C8B62@PU4P216MB1568.KORP216.PROD.OUTLOOK.COM>

?s 05:23 de 28/07/2024, akshay kulkarni escreveu:
> Dear members,
>                               WHy is the following code returning NA instead of the date?
> 
> 
>> as.Date("2022-01-02", origin = "1900-01-01",  format = "%y%d%m")
> [1] NA
> 
> 
> Thanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
Hello,

There are several reasons for your result.

1. You have 4 digits year but format %y (lower case = 2 digits year) It 
should be %Y
2. Your date has '-' as separator but your format doesn't have a separator.

Also, though less important:

1. You don't need argument origin. This is only needed with numeric to 
date coercion.
2. Are you sure the format is YYYY-DD-MM, year-day-month?


as.Date("2022-01-02", format = "%Y-%d-%m")
#> [1] "2022-02-01"

# note the origin is not your posted origin date,
# see the examples on Windows and Excel
# dates in help("as.Date")
as.Date(19024, origin = "1970-01-01")
#> [1] "2022-02-01"


Hope this helps,

Rui Barradas


-- 
Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus.
www.avg.com