Problem with as.POSIXct!!

Hi, could you explain why this happens?
And how to solve it?

> DT<-fread("42489	12/05/2020 23:59:53
+ 42490	12/05/2020 23:59:54
+ 42491	12/05/2020 23:59:55
+ 42492	12/05/2020 23:59:56
+ 42493	12/05/2020 23:59:57
+ 42494	12/05/2020 23:59:58
+ 42495	12/05/2020 23:59:59
+ 42496	13/05/2020 00:00:00
+ 42497	13/05/2020 00:00:01
+ 42498	13/05/2020 00:00:02
+ 42499	13/05/2020 00:00:03
+ 42500	13/05/2020 00:00:04
+ ")
> DT
       V1                  V2
 1: 42489 12/05/2020 23:59:53
 2: 42490 12/05/2020 23:59:54
 3: 42491 12/05/2020 23:59:55
 4: 42492 12/05/2020 23:59:56
 5: 42493 12/05/2020 23:59:57
 6: 42494 12/05/2020 23:59:58
 7: 42495 12/05/2020 23:59:59
 8: 42496 13/05/2020 00:00:00
 9: 42497 13/05/2020 00:00:01
10: 42498 13/05/2020 00:00:02
11: 42499 13/05/2020 00:00:03
12: 42500 13/05/2020 00:00:04
> str(DT)
Classes ‘data.table’ and 'data.frame':	12 obs. of  2 variables:
 $ V1: int  42489 42490 42491 42492 42493 42494 42495 42496 42497 42498 ...
 $ V2: chr  "12/05/2020 23:59:53" "12/05/2020 23:59:54" "12/05/2020 23:59:55" "12/05/2020 23:59:56" ...
 - attr(*, ".internal.selfref")=<externalptr> 
> DT[,"Time":=as.POSIXct(V2,tz="EST","%m/%d/%Y %H:%M:%S"),]
> DT
       V1                  V2                Time
 1: 42489 12/05/2020 23:59:53 2020-12-05 23:59:53
 2: 42490 12/05/2020 23:59:54 2020-12-05 23:59:54
 3: 42491 12/05/2020 23:59:55 2020-12-05 23:59:55
 4: 42492 12/05/2020 23:59:56 2020-12-05 23:59:56
 5: 42493 12/05/2020 23:59:57 2020-12-05 23:59:57
 6: 42494 12/05/2020 23:59:58 2020-12-05 23:59:58
 7: 42495 12/05/2020 23:59:59 2020-12-05 23:59:59
 8: 42496 13/05/2020 00:00:00                <NA>
 9: 42497 13/05/2020 00:00:01                <NA>
10: 42498 13/05/2020 00:00:02                <NA>
11: 42499 13/05/2020 00:00:03                <NA>
12: 42500 13/05/2020 00:00:04                <NA>
> str(DT)
Classes ‘data.table’ and 'data.frame':	12 obs. of  3 variables:
 $ V1  : int  42489 42490 42491 42492 42493 42494 42495 42496 42497 42498 ...
 $ V2  : chr  "12/05/2020 23:59:53" "12/05/2020 23:59:54" "12/05/2020 23:59:55" "12/05/2020 23:59:56" ...
 $ Time: POSIXct, format: "2020-12-05 23:59:53" "2020-12-05 23:59:54" ...
 - attr(*, ".internal.selfref")=<externalptr> 
> 

If the format is mdy then it's invalid for the m to be 13 as in your example.

the format is dmy!!!
the m is 05!!!
They are two consecutive days recorded every second.

My apologies, when changing the format to
"% d /% m /% Y% H:% M:% S".
All done!!!!
:pray: :+1:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.