2014-11-02 88 views
-1

什么是R找到时间差异的最快方式: 第1行到第2行,第2到第3,第3到第4行等等的差异。 或时间范围从1到2,2到3,...什么是找到时间差异最快的方法

最后,我标题为显示所有差异/时间跨度的函数,例如> 7000ms

HH:MM:SS:MIS * *毫秒 18:41:24.244 18:41:29.290 18:41:34.259 18:41:55.040 18:42:15.556 18:42:21.587 18:42:25.509 18:42:31.009 18:42:39.072 18:42:59.025 18:43:03.134 18:43:06.712 18:43:47.244 18:43:53.353 18:43:59.181 18:44:14.744 18:44:22.572 18:44:40.040 18:44:44.900 18:44:48.884 18:44:53.744 18:45:01.134 18:45:56.884 18:46:01.384 18:46:05.915 18:46:10.025 18:46:13.837 18:46:18.275 18:46:28.931 18:46:41.259 18:46:44.619 18:46:50.619

+0

你应该在这里找到好的提示:http://stackoverflow.com/questions/12611361/r-find-time-difference-in-seconds-for-yyyy-mm-dd-hhmmss-mmm-format,和这里:http://stackoverflow.com/questions/12649641/calculating-time-difference-in-r – Chase 2014-11-02 20:00:32

回答

3

目前尚不清楚你的数据是先从什么格式。我已经导入它作为一个特征向量:

head(times) 
# [1] "18:41:24.244" "18:41:29.290" "18:41:34.259" "18:41:55.040" ... 

然后,因为你想的差异,我们只要前面加上一个任意日期,并转换为POSIXct

times <- as.POSIXct(paste("2014-01-01",times),format="%Y-%m-%d %H:%M:%OS") 
diff(times) 
# Time differences in secs 
# [1] 5.046 4.969 20.781 20.516 6.031 3.922 5.500 8.063 19.953 4.109 ... 
3

与特征向量开始x

head(x) 
# [1] "18:41:24.244" "18:41:29.290" "18:41:34.259" "18:41:55.040" 
# [5] "18:42:15.556" "18:42:21.587" 

您可以使用strptimediff

st <- strptime(x, "%H:%M:%OS") 
st[diff(st) > 7] 
# [1] "2014-11-02 18:41:34.259 PST" "2014-11-02 18:41:55.040 PST" 
# [3] "2014-11-02 18:42:31.009 PST" "2014-11-02 18:42:39.072 PST" 
# [5] "2014-11-02 18:43:06.712 PST" "2014-11-02 18:43:59.181 PST" 
# [7] "2014-11-02 18:44:14.744 PST" "2014-11-02 18:44:22.572 PST" 
# [9] "2014-11-02 18:44:53.744 PST" "2014-11-02 18:45:01.134 PST" 
# [11] "2014-11-02 18:46:18.275 PST" "2014-11-02 18:46:28.931 PST" 
相关问题