【问题标题】:count unique cases that fall within overlapping posixct ymd hms intervals by the second按秒计算落在重叠 posixct ymd hms 间隔内的唯一案例
【发布时间】:2019-05-24 00:10:00
【问题描述】:

有一个 posixct 开始时间和结束时间,我正在尝试按秒计算具有重叠时间的项目的唯一情况

无法使用 lubridate 更改 posixct ymd hms 或将 hms 更改为奇数时间。数据在 mm:ss 中最初是一个字符。我在开头使用字符串操作添加了 00: 但无法进入正确的 hh:mm:ss 用于 dplyr 或有效的循环

`head(Fish_20180424_001_New) #my data table

      Survey ID          Clip ID                 Species         Enter Frame          Exit Frame
1: 20180424 001 20180424 001 001 Catostomus occidentalis 2019-05-23 00:01:03 2019-05-23 00:01:08
2: 20180424 001 20180424 001 001   Ptychocheilus grandis 2019-05-23 00:01:16 2019-05-23 00:01:18
3: 20180424 001 20180424 001 001 Catostomus occidentalis 2019-05-23 00:01:48 2019-05-23 00:01:52
4: 20180424 001 20180424 001 001 Catostomus occidentalis 2019-05-23 00:02:04 2019-05-23 00:02:06
5: 20180424 001 20180424 001 001                   Unkn. 2019-05-23 00:02:06 2019-05-23 00:02:08
6: 20180424 001 20180424 001 001   Ptychocheilus grandis 2019-05-23 00:02:07 2019-05-23 00:02:11      

setkey(Fish_20180424_001_New, Species, `Enter Frame`, `Exit Frame`)
vecTime <- seq(from=Fish_20180424_001_New$`Enter Frame`[1],to=Fish_20180424_001_New$`Exit Frame`[nrow(Fish_20180424_001_New)],by=1)
res <- data.frame(time=vecTime)

for(i in 1:length(unique(Fish_20180424_001_New$Species)) ) { 
addz <- array(0,length(vecTime))
remz <- array(0,length(vecTime))

Fish_20180424_001_New_Final <- Fish_20180424_001_New[J(unique(Fish_20180424_001_New$Species)[i]),] # Subset a certain species

startAgg <- aggregate(Fish_20180424_001_New_Final$`Enter Frame`,by=list(Fish_20180424_001_New_Final$`Enter Frame`),length)
endAgg <- aggregate(Fish_20180424_001_New_Final$`Exit Frame`,by=list(Fish_20180424_001_New_Final$`Exit Frame`),length)
addz[which(vecTime %in% startAgg$Group.1 )] <- startAgg$x
remz[which(vecTime %in% endAgg$Group.1)] <- -endAgg$x

res[,c( unique(Fish_20180424_001_New$Species)[i] )] <- cumsum(addz + remz)

}

res2<-res%>%filter(time < "2019-05-23 00:20:01")

max(res2$`Catostomus occidentalis`)
max(res2$`Micropterus sp`)
max(res2$`Micropeterus salmoides`)
max(res2$`Morone saxatilis`)
max(res2$`Ptychocheilus grandis`)
max(res2$Unidentified)
max(res2$Unkn.)

vecTime 的长度仅为 66,似乎从正确的位置 (00:01:03) 开始,但在第一个开始时间与结束时间 (02:06) 重叠的地方提前结束

需要获取适当的序列,该序列应在结束时间 00:51:27 的最后一个条目的长度处结束

【问题讨论】:

    标签: r


    【解决方案1】:

    通过将 vecTime 参数的第二部分 (to=) 更改为 max(exit frame),我能够获得适当的答案并消除开始和停止时间重叠的问题。

    如果有人想发表其他想法,仍然愿意探索其他答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2019-04-04
      相关资源
      最近更新 更多