[英]Convert data frame with epoch timestamps to time-series with milliseconds in R
I have the following data.frame: 我有以下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. 我已经尝试过
zoo
和xts
,但我发现很难将时代表示为日期。 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. 哪个返回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. 我也试过玩
options(digits.secs=3)
但没有运气。
I guess I'm hitting a small wall here with R's handling of milliseconds but any ideas would be greatly appreciated. 我想我在这里用R的处理毫秒来打一个小墙,但任何想法都会非常感激。
---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. 好的,感谢Joshua的回答和评论,这里将 @jirk Eddelbuettel的UNIX纪元转换为R中的Date对象 ,除以1000不会截断数据。 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
Your timestamps are in milliseconds. 您的时间戳以毫秒为单位。 You need to convert them to seconds to be able to use them with
as.POSIXct
. 您需要将它们转换为秒,以便能够将它们与
as.POSIXct
一起使用。 And there's no point in calling strptime
on a POSIXct vector. 在POSIXct向量上调用
strptime
没有意义。
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... 我不确定你为什么没有看到毫秒级的分辨率......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.