2017-10-13 17 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

시도해보십시오! 이 질문이 흥미로 우며 비슷한 것을 사용하여 스스로를 볼 수 있기 때문에 정말 대답을 찾고 싶었습니다. I 생각하면 가깝습니다.

의견보기

은 다음과 같습니다

  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