[R] Incorrect Conversion of Datetime

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Wed Jan 8 11:03:24 CET 2020


Hi Ogbos,
I get the correct result using strptime:

dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
 "%y %m %d %H"
)
> dta$date
[1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
[3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
[5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
[7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
[9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
[11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
[13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
[15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
[17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
[19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
[21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
[23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
[25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
[27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
[29] "1998-05-02 06:00:00 AEST"

Same result with

ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))

As it should be as the lower two call strptime. I can't see from your
code why the month and day are transcribed.

Jim



More information about the R-help mailing list