2017-10-13 40 views
0

我相信這不難做到,但我無法找到總結每小時繁殖時間的方法。我有成千上萬的事件數據(繁殖)鳥類到達(AR)和離開(LV)他們的巢穴。的相應時間arriving-和離開的事件在時間上給出這樣的:R日期時間每小時繁殖時間

times = as.POSIXct(c("2007-07-11 22:47:21 UTC", "2007-07-11 22:58:39 UTC", "2007-07-11 22:58:48 UTC", "2007-07-11 23:57:45 UTC", "2007-07-12 02:29:52 UTC", "2007-07-12 03:46:23 UTC", "2007-07-12 03:46:36 UTC", "2007-07-12 04:28:54 UTC", "2007-07-12 04:29:03 UTC", "2007-07-12 05:36:38 UTC"), tz = "UTC") 
breeding = c("AR", "LV", "AR", "LV", "AR", "LV", "AR", "LV", "AR", "OFF") 

現在我要計算的雀花什麼分數每小時自己的小窩,按小時休息這樣的

cut(times, breaks = "hour") 

只有右端也包括在內。

我試着總結difftime但當然它並沒有在小時和日期休息時減少。所以結果應該看起來像這樣:

Hour  fraction 

22-23  12min 

23-00  57min 

00-01   0min 

01-02   0min 

02-03  31min 

03-04  46min 

04-05   1min 

05-06  24min 

已經感謝了!

回答

0

來自dplyr,tidyrlubridate的解決方案。

如下

library(dplyr) 
library(tidyr) 
library(lubridate) 

dt2 <- dt %>% 
    mutate(breeding = ifelse(breeding %in% "OFF", "LV", breeding)) %>% 
    mutate(ID = rep(1:(n()/2), each = 2)) %>% 
    spread(breeding, times) %>% 
    mutate(DiffTime = difftime(LV, AR, "mins")) 
dt2 
    ID     AR     LV  DiffTime 
1 1 2007-07-11 22:47:21 2007-07-11 22:58:39 11.30000 mins 
2 2 2007-07-11 22:58:48 2007-07-11 23:57:45 58.95000 mins 
3 3 2007-07-12 02:29:52 2007-07-12 03:46:23 76.51667 mins 
4 4 2007-07-12 03:46:36 2007-07-12 04:28:54 42.30000 mins 
5 5 2007-07-12 04:29:03 2007-07-12 05:36:38 67.58333 mins 

如果你想知道最新的時刻,我們可以進一步提取日期和時間信息,如下所示,我們可以計算時間差。

dt3 <- dt2 %>% 
    mutate(AR_Date = as.Date(AR), LV_Date = as.Date(LV), 
     AR_Hour = hour(AR), LV_Hour = hour(LV)) 
dt3 

從這裏你可以決定如何進一步總結你的數據。

ID     AR     LV  DiffTime AR_Date LV_Date AR_Hour LV_Hour 
1 1 2007-07-11 22:47:21 2007-07-11 22:58:39 11.30000 mins 2007-07-11 2007-07-11  22  22 
2 2 2007-07-11 22:58:48 2007-07-11 23:57:45 58.95000 mins 2007-07-11 2007-07-11  22  23 
3 3 2007-07-12 02:29:52 2007-07-12 03:46:23 76.51667 mins 2007-07-12 2007-07-12  2  3 
4 4 2007-07-12 03:46:36 2007-07-12 04:28:54 42.30000 mins 2007-07-12 2007-07-12  3  4 
5 5 2007-07-12 04:29:03 2007-07-12 05:36:38 67.58333 mins 2007-07-12 2007-07-12  4  5 
0

試試這個!我真的想提出一個答案,因爲這很有趣,我可以看到自己使用類似的東西。我認爲它很接近。

看到你的想法!

是這樣的:

  Date hours Total 
1: 2007-07-11 22 01:10:15 
2: 2007-07-12  2 01:16:31 
3: 2007-07-12  3 00:42:18 
4: 2007-07-12  4 01:07:35 

所以,在22:00,他們將花費一個多小時。鳥類不會在2點前到達,也會花費一個多小時。

最後的輸出是times

#---Load the required libraries 
require(chron)    #---For time manipulation 
require(data.table)   #---Calculate and store data.tables 
require(splitstackshape) #---Split columns by a delimiter 

#---Input Data 
times = as.POSIXct(c("2007-07-11 22:47:21 UTC", "2007-07-11 22:58:39 UTC", "2007-07-11 22:58:48 UTC", "2007-07-11 23:57:45 UTC", "2007-07-12 02:29:52 UTC", "2007-07-12 03:46:23 UTC", "2007-07-12 03:46:36 UTC", "2007-07-12 04:28:54 UTC", "2007-07-12 04:29:03 UTC", "2007-07-12 05:36:38 UTC"), tz = "UTC") 
breeding = c("AR", "LV", "AR", "LV", "AR", "LV", "AR", "LV", "AR", "OFF") 

#---The times need to be a data.table for cSplit to work 
times <- as.data.table(times) 

#---This splits your time input into "Date" and "Time" columns 
times <- cSplit(times, "x", sep = " ") 
setnames(times, c("x_1", "x_2"),c("Date", "Time")) 
times[] <- lapply(times, as.character) 

#---Bring in "AR" or "LV" into a fresh data.table 
Data <- cbind(times, breeding) 

#---Format the time column for computation 
Data$Time <- chron(times = Data$Time, format = 'h:m:s') 

#rm(times,breeding) 

#---Arrival times 
arrive <- Data[breeding == "AR",] 
setnames(arrive, c("Time"), c("ArriveTime")) 
arrive[,3] <- NULL 

#---Departure times 
leave <- Data[breeding != "AR",] 
setnames(leave, c("Time"), c("LeaveTime")) 
leave[,c(1,3)] <- NULL 

#---But them together 
times <- cbind(arrive, leave) 
#rm(arrive, leave) 

#---Calculate elapsed times 
times <- times[, Elapsed := LeaveTime - ArriveTime] 
#rm(times3,Data) 

#---Sum elapsed time by the hour of the Arrival time 
times <- times[, .(Total = sum(Elapsed)), by = .(Date,hours(ArriveTime))] 
times