[R] Format

Rui Barradas
Sun, 09 Jun 2024 20:45:26 -0000

?s 21:39 de 09/06/2024, Val escreveu:
> HI all,
>
> My
> I am trying to convert character date (mm/dd/yy)  to YYYY-mm-dd date
> format in one of the columns of my data file.
>
> The first few lines of the data file looks like as follow
>
>   head(Atest,10);dim(Atest)
>        ddate
> 1  19/08/21
> 2  30/04/18
> 3  28/08/21
> 4  11/10/21
> 5  07/09/21
> 6  15/08/21
> 7  03/09/21
> 8  23/07/18
> 9  17/08/20
> 10 23/09/20
> [1] 1270076       1
>
> I am using the following different scenarios but none of them resulted
> the desired result.
>
> library(data.table)
> library(stringr)
> library(lubridate)
>          Atest$ddate1 <- as.Date((Atest$ddate), format =
"%m/%d/%y")
>          Atest$ddate2 <- mdy((Atest$ddate))
>          Atest$ddate3 <=
as.Date(as.character(Atest$ddate),format="%m/%d/%y")
>          Atest$ddate4 <-
as.Date(as.character(Atest$ddate),"%m/%d/%y")
>          Atest$ddate5 <- lubridate::mdy(Atest$ddate)
>
>> head(Atest,3)
>       ddate     ddate1 ddate2 ddate4 ddate5
> 1 19/08/21   <NA>   <NA>   <NA>   <NA>
> 2 30/04/18   <NA>   <NA>   <NA>   <NA>
> 3 28/08/21   <NA>   <NA>   <NA>   <NA>
>
>
> Any help why I am not getting the desired result.
> Thank you,
>
> ______________________________________________
> 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,

Day is clearly first, format "%m/%d/%y" assumes a month 19 in
19/08/21.
Try

as.Date(Atest$ddate, format = "%d/%m/%y")


Hope this helps,

Rui Barradas


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