2014-03-26 68 views
12

我有數據,其包括Date以及Time enterTime exit。這後兩種含有像這樣的數據:08:0212:0223:45R:如何處理沒有日期的時間?

我想操縱Time eXXX數據 - 從Time exit例如,Time enter。減去制定出的持續時間,或繪製的Time enterTime exit,例如分佈查看大部分參賽作品是否在10:00之前,或者大多數參賽作品是在17:00之後。

我看

,所有的包裝要求的日期先於時間,例如01/02/2012 12:33

這是可能的,或者我應該只是追加相同的日期,每次進行計算的緣故?這看起來有點凌亂!

回答

2

將類似的東西的工作?

SubstracTimes <- function(TimeEnter, TimeExit){ 
    (as.numeric(format(strptime(TimeExit, format ="%H:%M"), "%H")) + 
    as.numeric(format(strptime(TimeExit, format ="%H:%M"), "%M"))/60) - 
    (as.numeric(format(strptime(TimeEnter, format ="%H:%M"), "%H")) + 
    as.numeric(format(strptime(TimeEnter, format ="%H:%M"), "%M"))/60) 
} 

測試:

TimeEnter <- "08:02" 
TimeExit <- "12:02" 
SubstracTimes(TimeEnter, TimeExit) 
> SubstracTimes(TimeEnter, TimeExit) 
[1] 4 
+0

這適用於加/減,並返回小數小時(即3.5而不是3:30)。對於策劃,或許我可以手動爲了暗算時間輸入的頻率值分配給塊。 – gisol

+0

也許提供一些示例數據集並準確解釋您想要繪製的內容以及預期結果 –

13

使用在克隆氏病包中的類"times"

library(chron) 

Enter <- c("09:12", "17:01") 
Enter <- times(paste0(Enter, ":00")) 

Exit <- c("10:15", "18:11") 
Exit <- times(paste0(Exit, ":00")) 

Exit - Enter # durations 

sum(Enter < "10:00:00") # no entering before 10am 
mean(Enter < "10:00:00") # fraction entering before 10am 

sum(Exit > "17:00:00") # no exiting after 5pm 
mean(Exit > "17:00:00") # fraction exiting after 5pm 

table(cut(hours(Enter), breaks = c(0, 10, 17, 24))) # Counts for indicated hours 
## (0,10] (10,17] (17,24] 
##  1  1  0 

table(hours(Enter)) # Counts of entries each hour 
## 9 17 
## 1 1 

stem(hours(Enter), scale = 2) 
## The decimal point is at the | 

## 9 | 0 
## 10 | 
## 11 | 
## 12 | 
## 13 | 
## 14 | 
## 15 | 
## 16 | 
## 17 | 0 

顯卡:

tab <- c(table(Enter), -table(Exit)) # Freq at each time. Enter is pos; Exit is neg. 
plot(times(names(tab)), tab, type = "h", xlab = "Time", ylab = "Freq") 
abline(v = c(10, 17)/24, col = "red", lty = 2) # vertical red lines 
abline(h = 0) # X axis 

screenshot