基于观察时间的 +- x 小时过滤数据帧

Filter dataframe based on +- x hours around a time of observation

我有一个作为数据框读取的 ascii 文件,如下所示:

points <- data.frame(c(5.177, 6.57, 20.97, -1.18),
                       c(52.1, 46.49, 52.4, 60.14),
                       c("08:30:12", "09:45:20", "11:33:22", "14:12:43")
                       )

names(points) <- c('lon', 'lat', 'Time')

真实数据集约150000行,时间列为class"character"。

现在,我想做的是根据已知观察的时间段进行过滤。假设此观察发生在 11:00:00(所有时间均为 UTC),我想知道哪些测量值落在该已知观察的 3 小时内。

所以我需要过滤掉不在 11:00:00 的 1.5 小时 +- 范围内的值。

最后我想 select space 中最近的观察,这已经有效了。但是我无法根据这个时间限制找到如何 select 。最终函数看起来像这样:

nearestPixel <- function(matrix, station, time) {
  # filter based on time (3 hours around time of measurement)
  ## TODO ##

  # select just lat/lon from both datasets
  surfset <- SpatialPoints(surfmatrix[, 1:2])
  stationset <- SpatialPoints(station[, 2:3])

  # calculate closest point in distance
  nearest <- apply(gDistance(surfset, stationset, byid=TRUE), 1, which.min)

}

假设所有时间都来自同一日期,您可以使用以下代码代替#todo。我在数据中保留了Hour_Difference列仅供参考,如果不需要可以取消选择

library(lubridate)
points$Time<- hms(points$Time) # changing it to time

t1<-hms("11:00:00") #reference time as mentioned by you

points %>% 
 dplyr::mutate(Hour_Difference=hour(t1-Time)) %>% 
  dplyr::filter(abs(Hour_Difference)<3) %>% 
  dplyr::select(-Hour_Difference)

输出:

    lon   lat        Time Hour_Difference
1  6.57 46.49  9H 45M 20S               2
2 20.97 52.40 11H 33M 22S               0 

或者如果你想要一个班轮:

points %>% 
  dplyr::filter(abs(lubridate::hour(t1-Time))<3)