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

Convert value to time format in R

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

我认为由于没有 "seconds" 部分,您无法避免在此处进行一些解析,但即使使用简单的正则表达式也可以很容易地完成:

> 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"