R:转换不规则的时间字符串

Mar*_*mar 5 string time r time-series

我有两个不同的时间序列来自不同的数据帧,具有不同的不规则格式,但问题是相同的.我想只提取小时,分钟,秒和毫秒.

时代系列看起来像这样:

TS1

08:27:23,445
08:27:24,280
08:27:25,115
...
Run Code Online (Sandbox Code Playgroud)

我试过了

strptime("08:27:23,445", "%H:%M:%OS")
[1] "2013-05-23 08:27:23"
Run Code Online (Sandbox Code Playgroud)

我丢失了毫秒信息并得到了无用的(对我来说)日期信息.

TS2

Fri Apr 19 2013 08:39:41 GMT+0200
Fri Apr 19 2013 08:39:43 GMT+0200
Fri Apr 19 2013 08:39:45 GMT+0200
...
Run Code Online (Sandbox Code Playgroud)

我试过了

strptime("Fri Apr 19 2013 08:39:41 GMT+0200", "%a %b %d %Y %H:%M:%S %Z")
[1] NA
Run Code Online (Sandbox Code Playgroud)

最后,我想将ts1和ts2转换为具有相同格式(毫秒)的新时间序列,例如:

TS1

08:27:23,445
Run Code Online (Sandbox Code Playgroud)

TS2

08:39:41,000
Run Code Online (Sandbox Code Playgroud)

相同的格式对我来说很重要,因为我想稍后使用两个时间序列.例如:匹配时间序列,计算差异等...

谢谢您的帮助!

更新:添加dput

两个数据集都非常长,这就是为什么我试图削减它们.

TS1

structure(list(t = structure(1:9, .Label = c("08:27:23,445", 
                                                   "08:27:24,280", "08:27:25,115", "08:27:25,960", "08:27:26,780", 
                                                   "08:27:27,540", "08:27:28,295", "08:27:29,075", "08:27:29,910"), class = "factor")), .Names = "t", row.names = c(NA, -9L
                                                   ), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

TS2

structure(list(t = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 6L, 7L, 
                           8L), .Label = c("Fri Apr 19 2013 08:39:41 GMT+0200", "Fri Apr 19 2013 08:39:43 GMT+0200", 
                                           "Fri Apr 19 2013 08:39:45 GMT+0200", "Fri Apr 19 2013 08:39:49 GMT+0200", 
                                           "Fri Apr 19 2013 08:39:51 GMT+0200", "Fri Apr 19 2013 08:39:53 GMT+0200", 
                                           "Fri Apr 19 2013 08:39:59 GMT+0200", "Fri Apr 19 2013 08:40:05 GMT+0200", 
                                           "Fri Apr 19 2013 08:40:06 GMT+0200"
                           ), class = "factor")), .Names = "t", row.names = c(NA, -9L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

mCo*_*rey 1

如果您有置零点,下面是一个快速应用功能,可能会有所帮助。例如,如果您只想比较同一天从 0:00(午夜)到 23:59:99,999 的活动。如果是这样,您可以将时间转换为另一种形式(在我的示例中为分钟),然后您可以看到单个活动需要多长时间。

使用 t1 的示例:

制作时间向量(作为字符)

time <- c("08:27:23,445",
          "08:27:24,280",
          "08:27:25,115")
Run Code Online (Sandbox Code Playgroud)

将逗号更改为冒号,以便于剥离

time.new <- gsub(",", ":", time)
Run Code Online (Sandbox Code Playgroud)

计算小数分钟

time.mins <- sapply(strsplit(as.character(time.new), ":"),
                    function(x) {
                      x<-as.numeric(x)
                      (x[1]*60+x[2]+(x[3]/60)+(x[4]/60000))
                    })
Run Code Online (Sandbox Code Playgroud)

如果对列进行 df,结果如下所示:

> df <- cbind(time, time.mins)
> df
     time           time.mins         
[1,] "08:27:23,445" "507.39075"       
[2,] "08:27:24,280" "507.404666666667"
[3,] "08:27:25,115" "507.418583333333"
Run Code Online (Sandbox Code Playgroud)

我想这对于点击率之类的问题或者当您不关心超过 24 小时的总间隔时可能会更有帮助。