在R中将值转换为时间格式

时间:2015-12-11 23:02:37

标签: r time-series strptime

我的格式为“221559460”,其中前两个是小时,另外两个是分钟,后面的数字是以毫秒为单位。是否有任何快捷方法转换为时间序列格式而无需在值中手动追加“:”并从R中的毫秒数位计算秒数?

1 个答案:

答案 0 :(得分:3)

我认为你不能避免在这里进行一些解析,因为没有"秒"部分,但即使使用简单的正则表达式,它也可以相当容易地完成:

> sub(pattern     = '([0-9]{2})([0-9]{2})([0-9]{2})([0-9]*)',
+     replacement = '\\1:\\2:\\3.\\4',
+     x           = 221559460)
[1] "22:15:59.460"

> strptime(
+     x = sub(pattern     = '([0-9]{2})([0-9]{2})([0-9]{2})([0-9]*)',
+             replacement = '\\1:\\2:\\3.\\4',
+             x           = 221559460),
+     format = '%H:%M:%OS')
[1] "2015-12-11 22:15:59 PST"

根据以下评论更新了答案:

> options(digits.secs = 3)
> strptime(
+     x = sub(pattern     = '([0-9]{8})_([0-9]{2})([0-9]{2})([0-9]{2})([0-9]*)',
+             replacement = '\\1 \\2:\\3:\\4.\\5',
+             x           = '20150819_221559460'),
+     format = '%Y%m%d %H:%M:%OS')
[1] "2015-08-19 22:15:59.46 PDT"

矢量化示例的更新:

> df <- data.frame(timestamp = paste('20150819', 221559460 + round(runif(10) * 100), sep = '_'))
> strptime(sub('([0-9]{3})$', '.\\1', df$timestamp),'%Y%m%d_%H%M%OS')
 [1] "2015-08-19 22:15:59.517 PDT" "2015-08-19 22:15:59.550 PDT"
 [3] "2015-08-19 22:15:59.538 PDT" "2015-08-19 22:15:59.493 PDT"
 [5] "2015-08-19 22:15:59.484 PDT" "2015-08-19 22:15:59.549 PDT"
 [7] "2015-08-19 22:15:59.510 PDT" "2015-08-19 22:15:59.462 PDT"
 [9] "2015-08-19 22:15:59.466 PDT" "2015-08-19 22:15:59.474 PDT"