【问题标题】:How can I tell if a time point exists between a set of before and after times如何判断一组前后时间之间是否存在时间点
【发布时间】:2013-06-20 22:58:28
【问题描述】:

我试图回答一个关于堆栈溢出的问题 (Mapping multiple IDs using R) 时遇到了如何完成它的问题。即,如何测试一组前后时间点之间是否存在时间点。

帖子中的用户没有提供可重现的示例,但这是我想出的。我想用数据帧emtek_file 中的前后时间测试hidenic_file$hidenic_time 中的时间点,并返回与每个hidenic_id 的时间范围相匹配的emtek_id。发帖人没有提到它,但似乎每个hidenic_id 都有可能返回多个emtek_id

library(zoo)
date_string <- paste("2001", sample(12, 10, 3), sample(28,10), sep = "-")
time_string <- c("23:03:20", "22:29:56", "01:03:30", "18:21:03", "16:56:26",
                 "23:03:20", "22:29:56", "01:03:30", "18:21:03", "16:56:26")

entry_emtek <- strptime(paste(date_string, time_string), "%Y-%m-%d %H:%M:%S")
entry_emtek <- entry_emtek[order(entry_emtek)]
exit_emtek <- entry_emtek + 3600 * 24
emtek_file <- data.frame(emtek_id = 1:10, entry_emtek, exit_emtek)

hidenic_id <- 110380:110479
date_string <- paste("2001", sample(12, 100, replace = TRUE), sample(28,100, replace = T), sep = "-")
time_string <- rep(c("23:03:20", "22:29:56", "01:03:30", "18:21:03", "16:56:26",
                 "23:03:20", "22:29:56", "01:03:30", "18:21:03", "16:56:26"),10)
hidenic_time <- strptime(paste(date_string, time_string), "%Y-%m-%d %H:%M:%S")
hidenic_time <- hidenic_time[order(hidenic_time)]
hidenic_file <- data.frame(hidenic_id, hidenic_time)

##Here is where I fail to write concise and working code to find what I want. 
combined_file <- list() 
for(i in seq(hidenic_file[,1])) {
  for(j in seq(emtek_file[,1])) {
    if(length(zoo(1, emtek_file[j,2:3]) + zoo(1,hidenic_file[i,2])) == 0) {next}
    if(length(zoo(1, emtek_file[j,2:3]) + zoo(1,hidenic_file[i,2])) == 1) {combined_file[[i]] < c(combinedfile[[i]],emtek_file[j,1])}
  }
  names(combined_file)[i] <- hidenic_file[i,1]
}

【问题讨论】:

  • 您忘记了library(zoo),当我尝试运行您的循环时出现错误。添加预期结果对我们来说更容易:combined_file ?
  • 哎呀。它现在用 library(zoo) 编辑。我提到循环不起作用,但这是我解决问题的最佳尝试。请把最后一句话改写一下好吗?
  • 我对“这里是我没写简洁的地方”的理解,它有效但效率不高:)我的最后一句话,我的意思是预期的结果是什么?
  • 在我的示例中,我希望得到一个列表,其中每个元素都是一个单独的 hidenic id,并且在字符向量中具有匹配的 emtek id。我还没有添加列表中每个元素的名称。我将编辑以在循环之前添加它。

标签: r time-series zoo


【解决方案1】:

由于您没有提供预期的结果,因此我不确定您想要做什么。这是使用IRanges 包的解决方案。初读可能并不容易理解,但找到连续区间的重叠非常有用。

library(IRanges)
## create a time intervals 
subject <- IRanges(as.numeric(emtek_file$entry_emtek),
        as.numeric(emtek_file$exit_emtek))
## create a time intervals (start=end here)
query <- IRanges(as.numeric(hidenic_file$hidenic_time),
        as.numeric(hidenic_file$hidenic_time))
## find overlaps and extract rows (both time point and intervals)  
emt.ids <- subjectHits(findOverlaps(query,subject))
hid.ids <- queryHits(findOverlaps(query,subject))
cbind(hidenic_file[hid.ids,],emtek_file[emt.ids,])

 hidenic_id        hidenic_time emtek_id         entry_emtek          exit_emtek
8      110387 2001-03-13 22:29:56        3 2001-03-13 22:29:56 2001-03-14 22:29:56
9      110388 2001-03-14 01:03:30        3 2001-03-13 22:29:56 2001-03-14 22:29:56
41     110420 2001-06-09 16:56:26        7 2001-06-09 16:56:26 2001-06-10 16:56:26

Ps:安装包:

  source("http://bioconductor.org/biocLite.R")
  biocLite("IRanges")

【讨论】:

  • 我想我可以更具体地了解我想要的数据。我首先尝试得到一个真实的结果,然后将形状操纵到我想要的形状。感谢您向我介绍 IRange!
  • @cyclondude 欢迎您。结果的形状并不重要,重要的是预期的结果本身,hidenic_file 中的哪些 id,以及您期望得到的 emtek_file 中的哪些 id。
猜你喜欢
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多