2

I have a table (tags) with a column for timestamp (ts), which is formatted as seconds since 1 Jan, 1970 GMT. I'm trying to create a date column that converts the timestamp from seconds to date and time EST.

The suggested code for R was:

tags$date<-strptime(tags$ts, "%Y-%m-%d")
tags$date<-as.POSIXct(tags$date)

But when I do this, tags$date comes up as NA. Any suggestions for what I might be doing wrong? Thanks.

Pop
  • 12,135
  • 5
  • 55
  • 68
D.K.
  • 21
  • 1

2 Answers2

4

You should us as.POSIXct function instead:

tags$date <- as.POSIXct(tags$ts, origin="1970-01-01", tz="US/New York")

strptime converts between character representations and dates not between timestamp and dates.

Pop
  • 12,135
  • 5
  • 55
  • 68
  • 1
    Also required the time in timezone EST so would be something like tags$date <- as.POSIXct(tags$ts, origin="1970-01-01", tz="US/New York") – ngamita Oct 04 '17 at 11:57
  • You're right I forgot the timezone. Just edited my answer. Thanks – Pop Oct 04 '17 at 11:59
1

Here's a lubridate version. When we use as_datetime we don't need to explicitly specify an origin as it defaults to the desired origin.

lubridate::as_datetime(1507119276, tz='EST')
# [1] "2017-10-04 07:14:36 EST"
Paul
  • 8,734
  • 1
  • 26
  • 36