3
votes

Currently, I have date data stored in the column of a large data.table table. The date data look something like:

dt1 <- data.table(Person = seq(6), Date = ymd_hms(c("2007-1-1 12:31:25 PST", "2007-1-3 09:31:25 PST", "2007-1-9 15:31:22 PST", "2007-1-17 21:41:23 PST", "2007-1-30 02:04:22 PST", "2007-2-2 02:07:12 PST")))

> dt1
Person                Date
1:      1 2007-01-01 12:31:25
2:      2 2007-01-03 09:31:25
3:      3 2007-01-09 15:31:22
4:      4 2007-01-17 21:41:23
5:      5 2007-01-30 02:04:22
6:      6 2007-02-02 02:07:12

ymd_hms() is a function in the package lubridate. Now, when I convert the dates to numeric as so, I get:

dt1[,Date:=as.numeric(Date)]

> dt1
Person       Date
1:      1 1167654685
2:      2 1167816685
3:      3 1168356682
4:      4 1169070083
5:      5 1170122662
6:      6 1170382032

After this, I have tried various methods but have been unable to convert the dates back to the original format. Would anyone know of a simple function? Thank you!

1
I'm not sure how to turn this into a lubridate Answer of the top of my head, but essentially dt1[, as.POSIXct(Date, origin="1970-01-01", tz="UTC")]GSee

1 Answers

2
votes

I looked through ?lubridate, and I don't see any mention of support for converting numeric to POSIXct.

So, I think you "have" to do it with base R.

Date = ymd_hms(c("2007-1-1 12:31:25 PST", "2007-1-3 09:31:25 PST", 
                 "2007-1-9 15:31:22 PST", "2007-1-17 21:41:23 PST", 
                 "2007-1-30 02:04:22 PST", "2007-2-2 02:07:12 PST"))
as.numeric(Date)
#[1] 1167654685 1167816685 1168356682 1169070083 1170122662 1170382032
as.POSIXct(as.numeric(Date), origin="1970-01-01", tz="UTC")
#[1] "2007-01-01 12:31:25 UTC" "2007-01-03 09:31:25 UTC"
#[3] "2007-01-09 15:31:22 UTC" "2007-01-17 21:41:23 UTC"
#[5] "2007-01-30 02:04:22 UTC" "2007-02-02 02:07:12 UTC"

As a side note, your times are being created in the UTC, not "PST". If that's not desireable, maybe you meant to include tz=America/Los_Angeles in the ymd_hms() call

Date <- ymd_hms(c("2007-1-1 12:31:25 PST", "2007-1-3 09:31:25 PST", 
                  "2007-1-9 15:31:22 PST", "2007-1-17 21:41:23 PST", 
                  "2007-1-30 02:04:22 PST", "2007-2-2 02:07:12 PST"),
                tz="America/Los_Angeles")

#Sys.setenv(TZ="America/Los_Angeles") 
as.POSIXct(as.numeric(Date), origin="1970-01-01")
#[1] "2007-01-01 12:31:25 PST" "2007-01-03 09:31:25 PST"
#[3] "2007-01-09 15:31:22 PST" "2007-01-17 21:41:23 PST"
#[5] "2007-01-30 02:04:22 PST" "2007-02-02 02:07:12 PST"