当我遇到如何完成它时,我试图回答一个关于堆栈溢出的问题(使用 R 映射多个 ID )。即,如何测试一组前后时间点之间是否存在时间点。
帖子中的用户没有提供可重复的示例,但这是我想出的。我想用数据帧中hidenic_file$hidenic_time
的前后时间测试时间点,emtek_file
并返回与emtek_id
每个时间帧匹配的时间点hidenic_id
。发帖人没有提到它,但似乎emtek_id
每个人都有可能返回多个hidenic_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]
}