2015-09-11 52 views
0

我有一個R數據幀和6列,以及如何提取09:16:00.00和09:30:00.00之間的數據。R數據幀中的時間數據提取

輸入

20140901 09:16:00.00 7994.70 7997.50 7984.00 7996.60 
20140901 09:17:00.00 7995.85 7999.10 7992.40 7997.00 
20140901 09:18:00.00 7996.70 8005.20 7996.40 8004.00 
20140901 09:19:00.00 8004.50 8007.95 8003.95 8004.00 
20140901 09:20:00.00 8004.30 8005.70 7998.90 7998.95 
20140901 09:21:00.00 7998.55 7998.70 7994.50 7995.35 
20141031 09:30:00.00 8218.70 8221.85 8218.65 8221.70 
20141031 09:34:00.00 8221.60 8225.10 8221.60 8224.80 
20141031 09:35:00.00 8224.75 8228.55 8224.70 8227.65 
20141031 09:36:00.00 8227.85 8231.25 8227.50 8230.40 
20141031 09:37:00.00 8231.00 8237.30 8230.35 8235.95 
20141031 09:38:00.00 8236.25 8241.50 8233.60 8234.40 
20141031 09:39:00.00 8234.75 8235.00 8229.00 8229.10 

輸出

20140901 09:16:00.00 7994.70 7997.50 7984.00 7996.60 
20140901 09:17:00.00 7995.85 7999.10 7992.40 7997.00 
20140901 09:18:00.00 7996.70 8005.20 7996.40 8004.00 
20140901 09:19:00.00 8004.50 8007.95 8003.95 8004.00 
20140901 09:20:00.00 8004.30 8005.70 7998.90 7998.95 
20140901 09:21:00.00 7998.55 7998.70 7994.50 7995.35 
20141031 09:30:00.00 8218.70 8221.85 8218.65 8221.70 
+1

'dput()'output>原始列文本轉儲 – hrbrmstr

回答

3

這是一個基本的選擇過程。 dplyr或任何擴展包真的沒有必要。你只需要指定一個任意參考一天,這樣的日期/時間對彼此適當相比:

times <- as.POSIXct(paste("1970-01-01",dat$V2),tz="UTC") 
dat[ 
    times >= as.POSIXct("1970-01-01 09:16",tz="UTC") & 
    times <= as.POSIXct("1970-01-01 09:30",tz="UTC"), 
] 

#  V1   V2  V3  V4  V5  V6 
#1 20140901 09:16:00.00 7994.70 7997.50 7984.00 7996.60 
#2 20140901 09:17:00.00 7995.85 7999.10 7992.40 7997.00 
#3 20140901 09:18:00.00 7996.70 8005.20 7996.40 8004.00 
#4 20140901 09:19:00.00 8004.50 8007.95 8003.95 8004.00 
#5 20140901 09:20:00.00 8004.30 8005.70 7998.90 7998.95 
#6 20140901 09:21:00.00 7998.55 7998.70 7994.50 7995.35 
#7 20141031 09:30:00.00 8218.70 8221.85 8218.65 8221.70 

如果你感覺你膽子可能下降的tz=和指定的日期和做這一切在一個去:

transform(dat, times = as.POSIXct(dat$V2,format="%H:%M:%S"))[ 
    times >= as.POSIXct("09:16",format="%H:%M") & 
    times <= as.POSIXct("09:30",format="%H:%M"), 
] 

唯一的邊緣的情況下,我能想到的,其中後一種情況下可能會中斷是,如果它被設置在下午11:59:59運行,並且後者as.POSIXct調用一個前打勾到第二天被製造了。當沒有日期的時間由as.POSIXct指定時,它僅替換當前系統日期。

+0

沒有dplyr效果不錯,性能也不錯。 – Gower

1

dplyr操作完美的候選人:

dat <- read.table(text="20140901 09:16:00.00 7994.70 7997.50 7984.00 7996.60 
20140901 09:17:00.00 7995.85 7999.10 7992.40 7997.00 
20140901 09:18:00.00 7996.70 8005.20 7996.40 8004.00 
20140901 09:19:00.00 8004.50 8007.95 8003.95 8004.00 
20140901 09:20:00.00 8004.30 8005.70 7998.90 7998.95 
20140901 09:21:00.00 7998.55 7998.70 7994.50 7995.35 
20141031 09:30:00.00 8218.70 8221.85 8218.65 8221.70 
20141031 09:34:00.00 8221.60 8225.10 8221.60 8224.80 
20141031 09:35:00.00 8224.75 8228.55 8224.70 8227.65 
20141031 09:36:00.00 8227.85 8231.25 8227.50 8230.40 
20141031 09:37:00.00 8231.00 8237.30 8230.35 8235.95 
20141031 09:38:00.00 8236.25 8241.50 8233.60 8234.40 
20141031 09:39:00.00 8234.75 8235.00 8229.00 8229.10", 
        header=FALSE, stringsAsFactors=FALSE) 


library(dplyr) 

dat %>% 
    mutate(timestamp=as.POSIXct(sprintf("%s %s", V1, V2), 
           format="%Y%m%d %H:%M:%S")) %>% # real timestamps 
    group_by(V1) %>%           # perform operation by day 
    filter(timestamp>=as.POSIXct(sprintf("%s 09:16:00.00", V1), 
           format="%Y%m%d %H:%M:%S")) %>% # >= first HMS 
    filter(timestamp<=as.POSIXct(sprintf("%s 09:30:00.00", V1), 
           format="%Y%m%d %H:%M:%S")) %>% # <= last HMS 
    ungroup 

## Source: local data frame [7 x 7] 
## 
##   V1   V2  V3  V4  V5  V6   timestamp 
##  (int)  (chr) (dbl) (dbl) (dbl) (dbl)    (time) 
## 1 20140901 09:16:00.00 7994.70 7997.50 7984.00 7996.60 2014-09-01 09:16:00 
## 2 20140901 09:17:00.00 7995.85 7999.10 7992.40 7997.00 2014-09-01 09:17:00 
## 3 20140901 09:18:00.00 7996.70 8005.20 7996.40 8004.00 2014-09-01 09:18:00 
## 4 20140901 09:19:00.00 8004.50 8007.95 8003.95 8004.00 2014-09-01 09:19:00 
## 5 20140901 09:20:00.00 8004.30 8005.70 7998.90 7998.95 2014-09-01 09:20:00 
## 6 20140901 09:21:00.00 7998.55 7998.70 7994.50 7995.35 2014-09-01 09:21:00 
## 7 20141031 09:30:00.00 8218.70 8221.85 8218.65 8221.70 2014-10-31 09:30:00