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
  • 11,817
  • 4
  • 53
  • 67
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
  • 11,817
  • 4
  • 53
  • 67
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
  • 7,801
  • 1
  • 22
  • 34