2014-11-02 98 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" 
相關問題