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

William Dunlap wdun|@p @end|ng |rom t|bco@com
Tue Jun 23 21:20:35 CEST 2020


When you give an example it really helps to (a) show the data as the output
of dput() or dump() so one can copy and paste into R and (b) show the
result (the wrong value or error message) that you got.

You example is missing some quotes and has an unneeded call to lapply().

> dump("Data", file=stdout())
Data <-
structure(list(Id = c("INCBR0005072277", "INCBR0005073034",
"INCBR0005073131",
"INCBR0005074186", "INCBR0005074188"), Timestamp = c(1577059200000,
1577059200000, 1577059200000, 1577059200000, 1577059200000),
    `Data Type` = c("itsm-ticket", "itsm-ticket", "itsm-ticket",
    "itsm-ticket", "itsm-ticket"), Visibility = c("U&FOUO&USA",
    "U&FOUO&USA", "U&FOUO&USA", "U&FOUO&USA", "U&FOUO&USA"),
    TYPE_SERVER = c(0L, 1L, 0L, 0L, 0L)), row.names = c(NA, -5L
), class = "data.frame")
> str(as.POSIXct(Data$Timestamp/1000,
origin=as.POSIXct("1970-01-01",tz="UTC"), tz="UTC"))
 POSIXct[1:5], format: "2019-12-23" "2019-12-23" "2019-12-23" "2019-12-23"
"2019-12-23"

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Jun 23, 2020 at 11:24 AM Gregg via R-help <r-help using r-project.org>
wrote:

> 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______________________________________________
> R-help using 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list