R 标识一个数据集中处于另一个数据集中两个时间间隔的记录

R 标识一个数据集中处于另一个数据集中两个时间间隔的记录,r,gpx,R,Gpx,我在一个数据帧中有GPX数据,在另一个数据帧中我调用了info_access,其中包含我希望与GPX数据“合并”的其他信息。数据帧之间没有公共变量。我想在第二个数据帧(信息访问)中使用TowStartDate、斜面开始(时间)、TowEndDate和斜面开始(时间)列来标识GPX数据帧中日期/时间介于TowStartDate和斜面开始与TowEndDate和斜面开始之间的行。然后我想将这些GPX乘以第二个数据帧的Tow值。GPX数据集很大,所以我最初尝试的方式遇到了问题 GPX数据示例: exa

我在一个数据帧中有GPX数据,在另一个数据帧中我调用了info_access,其中包含我希望与GPX数据“合并”的其他信息。数据帧之间没有公共变量。我想在第二个数据帧(信息访问)中使用TowStartDate、斜面开始(时间)、TowEndDate和斜面开始(时间)列来标识GPX数据帧中日期/时间介于TowStartDate和斜面开始与TowEndDate和斜面开始之间的行。然后我想将这些GPX乘以第二个数据帧的Tow值。GPX数据集很大,所以我最初尝试的方式遇到了问题

GPX数据示例:

example_gpx<-data.frame(Long=c(-70.92108,-70.92108,-70.92108, -70.92108, -70.92108 ),
    Lat=c(41.64437,41.64437,41.64437 ,41.64437,41.64437),
    Date=c("2016-06-04","2016-06-04","2016-06-04","2016-06-04","2016-06-04"),
    Time=c("19:15:08","19:15:09","19:15:10","19:15:11","19:15:12"))
这在我的小示例中是有效的,但是如果有人知道如何使用包含88287个观测值的GPX文件来实现这一点,那么这将非常有用,或者是一个更优雅的解决方案

 sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.3     gmt_1.2-0       RODBC_1.3-12    lubridate_1.5.0
[5] rgdal_1.0-6     maptools_0.8-36 sp_1.1-1       

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6     lattice_0.20-31 assertthat_0.1  grid_3.2.1     
 [5] R6_2.1.0        DBI_0.3.1       magrittr_1.5    stringi_0.5-5  
 [9] tools_3.2.1     stringr_1.0.0   foreign_0.8-63  parallel_3.2.1 
> 

下面是我使用
lubridate
包提出的一个解决方案。请注意,我更改了您的示例数据,以便将间隔排列起来,并且它实际上能够为GPX数据分配两个值。
示例_访问
未更改

library(lubridate)

example_gpx<-data.frame(Long=c(-70.92108,-70.92108,-70.92108, -70.92108, -70.92108 ),
                        Lat=c(41.64437,41.64437,41.64437 ,41.64437,41.64437),
                        Date=c("2016-06-04","2016-06-04","2016-06-04","2016-06-04","2016-06-04"),
                        Time=c("14:15:08","15:15:09","15:59:10","17:06:11","17:30:12"))

example_access$date_time_start<-ymd_hms(example_access$date_time_start)
example_access$date_time_end<-ymd_hms(example_access$date_time_end)

example_gpx$date_time<-paste(example_gpx$Date, example_gpx$Time)
example_gpx$date_time<-ymd_hms(example_gpx$date_time)

example_gpx$Tow<-sapply(1:nrow(example_gpx), function(x)
  if(example_gpx[x,]$date_time %between%  
     c(example_access[x,]$date_time_start,example_access[x,]$date_time_end)) example_access[x,]$Tow
      else NA)

example_gpx

Long   Lat       Date     Time           date_time       Tow
-70.92 41.64 2016-06-04 14:15:08 2016-06-04 14:15:08 201604001
-70.92 41.64 2016-06-04 15:15:09 2016-06-04 15:15:09 201604002
-70.92 41.64 2016-06-04 15:59:10 2016-06-04 15:59:10 201604003
-70.92 41.64 2016-06-04 17:06:11 2016-06-04 17:06:11 201604004
-70.92 41.64 2016-06-04 17:30:12 2016-06-04 17:30:12 201604005
库(lubridate)

示例\u gpx这里是我使用
lubridate
包提出的一个解决方案。请注意,我更改了您的示例数据,以便将间隔排列起来,并且它实际上能够为GPX数据分配两个值。
示例_访问
未更改

library(lubridate)

example_gpx<-data.frame(Long=c(-70.92108,-70.92108,-70.92108, -70.92108, -70.92108 ),
                        Lat=c(41.64437,41.64437,41.64437 ,41.64437,41.64437),
                        Date=c("2016-06-04","2016-06-04","2016-06-04","2016-06-04","2016-06-04"),
                        Time=c("14:15:08","15:15:09","15:59:10","17:06:11","17:30:12"))

example_access$date_time_start<-ymd_hms(example_access$date_time_start)
example_access$date_time_end<-ymd_hms(example_access$date_time_end)

example_gpx$date_time<-paste(example_gpx$Date, example_gpx$Time)
example_gpx$date_time<-ymd_hms(example_gpx$date_time)

example_gpx$Tow<-sapply(1:nrow(example_gpx), function(x)
  if(example_gpx[x,]$date_time %between%  
     c(example_access[x,]$date_time_start,example_access[x,]$date_time_end)) example_access[x,]$Tow
      else NA)

example_gpx

Long   Lat       Date     Time           date_time       Tow
-70.92 41.64 2016-06-04 14:15:08 2016-06-04 14:15:08 201604001
-70.92 41.64 2016-06-04 15:15:09 2016-06-04 15:15:09 201604002
-70.92 41.64 2016-06-04 15:59:10 2016-06-04 15:59:10 201604003
-70.92 41.64 2016-06-04 17:06:11 2016-06-04 17:06:11 201604004
-70.92 41.64 2016-06-04 17:30:12 2016-06-04 17:30:12 201604005
库(lubridate)

示例\u gpxI可能遗漏了一些内容,但看起来您没有任何符合访问间隔的gpx观察结果。所有的gpx时间都是19:00。。。最新的倾斜时间是17:00。。。你的方法工作正常吗?我可能遗漏了一些东西,但看起来你没有任何符合访问间隔的gpx观察结果。所有的gpx时间都是19:00。。。最新的倾斜时间是17:00。。。你的方法正确吗?嗨,对不起,我不在办公室。更正时间后,您的代码在示例数据集上工作。当我在我的大数据集上运行它时,我收到以下错误消息:在%c之间的if(info_gpx[x,]$Date\u time%中出错(info_access[x,]$date\U time\U start,:缺少需要TRUE/FALSE的值。您认为这是因为数据凌乱吗?实际上,我认为
%before%
来自
data.table
包。请尝试
库(data.table)
并查看其是否有效。此外,如果尚未安装,请确保安装
data.table
。请查看我刚才添加的编辑代码。它应该比以前的回答工作得更好。抱歉,现在我收到一条不同的错误消息:
中的错误$you run
示例\u gpx$TowHi-抱歉,我不在办公室。您的代码在您更正时间后处理示例数据集。当我在我的大型数据集上运行它时,会收到以下错误消息:if(info_gpx[x,]$Date\u time%在%c之间出错(info_access[x,]$date\U time\U start,:缺少需要TRUE/FALSE的值。您认为这是因为数据凌乱吗?实际上,我认为
%before%
来自
data.table
包。请尝试
库(data.table)
并查看其是否有效。此外,如果尚未安装,请确保安装
data.table
。请查看我刚才添加的已编辑代码。它应该比以前的回答工作得更好。抱歉,现在我收到一条不同的错误消息:
$中的错误您是否运行了
示例\u gpx$Tow
library(lubridate)

example_gpx<-data.frame(Long=c(-70.92108,-70.92108,-70.92108, -70.92108, -70.92108 ),
                        Lat=c(41.64437,41.64437,41.64437 ,41.64437,41.64437),
                        Date=c("2016-06-04","2016-06-04","2016-06-04","2016-06-04","2016-06-04"),
                        Time=c("14:15:08","15:15:09","15:59:10","17:06:11","17:30:12"))

example_access$date_time_start<-ymd_hms(example_access$date_time_start)
example_access$date_time_end<-ymd_hms(example_access$date_time_end)

example_gpx$date_time<-paste(example_gpx$Date, example_gpx$Time)
example_gpx$date_time<-ymd_hms(example_gpx$date_time)

example_gpx$Tow<-sapply(1:nrow(example_gpx), function(x)
  if(example_gpx[x,]$date_time %between%  
     c(example_access[x,]$date_time_start,example_access[x,]$date_time_end)) example_access[x,]$Tow
      else NA)

example_gpx

Long   Lat       Date     Time           date_time       Tow
-70.92 41.64 2016-06-04 14:15:08 2016-06-04 14:15:08 201604001
-70.92 41.64 2016-06-04 15:15:09 2016-06-04 15:15:09 201604002
-70.92 41.64 2016-06-04 15:59:10 2016-06-04 15:59:10 201604003
-70.92 41.64 2016-06-04 17:06:11 2016-06-04 17:06:11 201604004
-70.92 41.64 2016-06-04 17:30:12 2016-06-04 17:30:12 201604005
    library(lubridate)
library(data.table)

example_gpx<-data.frame(Long=c(-70.92108,-70.92108,-70.92108, -70.92108, -70.92108 ),
                        Lat=c(41.64437,41.64437,41.64437 ,41.64437,41.64437),
                        Date=c("2016-06-04","2016-06-04","2016-06-04","2016-06-04","2016-06-04"),
                        Time=c("14:15:08","15:15:09","15:59:10","17:06:11","17:30:12"))

example_access$date_time_start<-ymd_hms(example_access$date_time_start)
example_access$date_time_end<-ymd_hms(example_access$date_time_end)

example_gpx$date_time<-paste(example_gpx$Date, example_gpx$Time)
example_gpx$date_time<-ymd_hms(example_gpx$date_time)

example_gpx$Tow<-NA

for(x in 1:nrow(example_access)){
  example_gpx[which(example_gpx$date_time %between% 
                      c(example_access[x,]$date_time_start,
                        example_access[x,]$date_time_end)),]$Tow<-example_access[x,]$Tow
}
example_gpx

Long   Lat       Date     Time           date_time       Tow
-70.92 41.64 2016-06-04 14:15:08 2016-06-04 14:15:08 201604001
-70.92 41.64 2016-06-04 15:15:09 2016-06-04 15:15:09 201604002
-70.92 41.64 2016-06-04 15:59:10 2016-06-04 15:59:10 201604003
-70.92 41.64 2016-06-04 17:06:11 2016-06-04 17:06:11 201604004
-70.92 41.64 2016-06-04 17:30:12 2016-06-04 17:30:12 201604005