[R] How to convert column from millisecond epoch time to yyyy-mm-dd GMT

Enrico Schumann e@ @end|ng |rom enr|co@chum@nn@net
Tue Jun 23 21:09:44 CEST 2020


On Tue, 23 Jun 2020, Gregg via R-help writes:

> Hello to all the smart people out there....
>
> I have a data.frame labeled itsm_service_type_field. I need to convert
> the Timestamp field which is epoch time in milliseconds to a
> yyyy-mm-dd GMT Date. 
>
> Data.frame format is below.
>
> I've attempted to use the lapply and as.POSIXct
> functions to convert the time field in the original
> data.frame to a new data.frame I've labeled
> "itsm_service_type_field_adjusted_time",
> but I've got the order and syntax wrong.
>
> Help would be so much appreciated.
>
> Thanks in advance.
> Gregg
> Arizona
>
> Details - See Below:
>
> itsm_service_type_field <- fread("itsm_service_type_2018-2019_CONUS.csv")
>
>>>>>>>>>>>>>>???????????itsm_service_type_field_adjusted_time <- lapply(itsm_service_type_field[ , Timestamp], as.POSIXct(Timestamp, origin="1970-01-01", tz="GMT"))
>
> head(itsm_service_type_field)
>     Id                              Timestamp         Data Type Visibility                      TYPE_SERVICE
> 1: INCBR0005072277 1577059200000 itsm-ticket U&FOUO&USA            0
> 2: INCBR0005073034 1577059200000 itsm-ticket U&FOUO&USA            1
> 3: INCBR0005073131 1577059200000 itsm-ticket U&FOUO&USA            0
> 4: INCBR0005074186 1577059200000 itsm-ticket U&FOUO&USA            0
> 5: INCBR0005074188 1577059200000 itsm-ticket U&FOUO&USA            0
> 6: INCBR0005074546 1577059200000 itsm-ticket U&FOUO&USA            0
>

You should divide by 1000:

    .POSIXct(1577059200000/1000, tz = "GMT")
    ## [1] "2019-12-23 GMT"

    as.POSIXct(1577059200000/1000,
               origin = as.POSIXct("1970-01-01 00:00:00", tz = "GMT"),
               tz = "GMT")
    ## [1] "2019-12-23 GMT"


-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-help mailing list