Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert data frame with epoch timestamps to time-series with milliseconds in R

I have the following data.frame:

df <- data.frame(timestamp=c(1428319770511, 1428319797218, 1428319798182, 1428319803327, 1428319808478),
                 session=c("A","A","B","A","A"))

I'd like to convert this data frame to a time series and work on time windows shorter than one second. I already tried zoo and xts, but I found it difficult to represent the epoch times as dates. Here's what I already tried:

df$date<-strptime(as.POSIXct(df$timestamp, origin="1970-01-01"),format="%Y-%m-%d %H:%M:%OS")

Which return NAs. Calling this:

df$date<-strptime(as.POSIXct(df$timestamp/1000, origin="1970-01-01"),format="%Y-%m-%d %H:%M:%OS")

Works but doesn't contain milliseconds data. I also tried to play with options(digits.secs=3) but with no luck.

I guess I'm hitting a small wall here with R's handling of milliseconds but any ideas would be greatly appreciated.

---EDIT---

Ok, Thanks to Joshua's answer and a comment here Convert UNIX epoch to Date object in R by @Dirk Eddelbuettel, dividing by 1000 doesn't truncate the data. So this works:

options(digits.secs = 3)
df$date<-as.POSIXct(df$timestamp/1000, origin="1970-01-01", tz="UTC")

Which returns:

timestamp       session date    
1428319770511   A       2015-04-06 14:29:30.510
1428319797218   A       2015-04-06 14:29:57.217
1428319798182   B       2015-04-06 14:29:58.181
1428319803327   A       2015-04-06 14:30:03.326
1428319808478   A       2015-04-06 14:30:08.477
like image 969
Omri374 Avatar asked Oct 20 '22 11:10

Omri374


1 Answers

Your timestamps are in milliseconds. You need to convert them to seconds to be able to use them with as.POSIXct. And there's no point in calling strptime on a POSIXct vector.

Also, it's good practice to explicitly set the timezone, rather than leave it set to "".

df$datetime <- as.POSIXct(df$timestamp/1000, origin="1970-01-01", tz="UTC")
options(digits.secs=6)
df
#     timestamp session                datetime
# 1 1.42832e+12       A 2015-04-06 11:29:30.510
# 2 1.42832e+12       A 2015-04-06 11:29:57.217
# 3 1.42832e+12       B 2015-04-06 11:29:58.181
# 4 1.42832e+12       A 2015-04-06 11:30:03.326
# 5 1.42832e+12       A 2015-04-06 11:30:08.477

I'm not sure why you aren't seeing millisecond resolution...

like image 118
Joshua Ulrich Avatar answered Oct 22 '22 00:10

Joshua Ulrich