Home > Software design >  Convert character to date or date time vector without knowing its origin or format
Convert character to date or date time vector without knowing its origin or format

Time:09-04

I suspect that I have a datetime vector like:

timestamp = c(4273639583, 4273639583, 4273639583, 4273639583, 4273639583)

how can I convert it to datetime if I do not know for sure that it is a datetime vector or just date. Also I do not know its origin.

timestamp <- as.POSIXct(timestamp, format="%m/%d/%Y %H:%M", tz=Sys.timezone())

CodePudding user response:

You could use the package anytime with anytime function like this:

dt<-structure(list(timestamp = c(4273639583, 4273639583, 4273639583, 
                                 4273639583, 4273639583)), row.names = c(NA, -5L), class = c("tbl_df", 
                                                                                             "tbl", "data.frame"))

library(anytime)
anytime(dt$timestamp)
#> [1] "2105-06-05 12:06:23 CEST" "2105-06-05 12:06:23 CEST"
#> [3] "2105-06-05 12:06:23 CEST" "2105-06-05 12:06:23 CEST"
#> [5] "2105-06-05 12:06:23 CEST"

Created on 2022-09-03 with reprex v2.0.2

  •  Tags:  
  • r
  • Related