Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于站点和R中最近时间合并两个数据集_R_Time_Merge_Location - Fatal编程技术网

基于站点和R中最近时间合并两个数据集

基于站点和R中最近时间合并两个数据集,r,time,merge,location,R,Time,Merge,Location,我有一个文件,显示了一个物种在不同地点被发现的时间(出现时间),我想将其与该地点最近记录的gps位置(GPSTime)合并。我在下面提供了一个例子。实际数据集相当大 Abundance Site TimeofOccurance Species 1 11:00 a 1 12:00 b 2 14:05 a 3 16:00

我有一个文件,显示了一个物种在不同地点被发现的时间(出现时间),我想将其与该地点最近记录的gps位置(GPSTime)合并。我在下面提供了一个例子。实际数据集相当大

Abundance
Site     TimeofOccurance     Species
1         11:00               a
1         12:00               b
2         14:05               a
3         16:00               c
这将产生以下理想输出:

AbundanceGPS
Site     TimeofOccurance       Species     Longitude     Latitude
1         11:00                 a          X1            Y1
1         12:00                 b          X3            Y3
2         14:05                 a          X5            Y5
3         16:00                 c          X8            Y8
这是否有效:

library(dplyr)
library(stringr)
library(lubridate)
Abundance %>% 
   inner_join(GPS %>% 
                mutate(nearest_time = str_extract(floor_date(lubridate::ymd_hms(str_c(Sys.Date(), `GPS Time`, sep = ' ')), unit = '5 mins'), '..:..:..')), 
              by = c('TimeofOccurance' = 'nearest_time')) %>% select('Site' = Site.x, TimeofOccurance, Species, Longitude, Latitude)
# A tibble: 4 x 5
   Site TimeofOccurance Species Longitude Latitude
  <dbl> <time>          <chr>   <chr>     <chr>   
1     1 11:00           a       X1        Y1      
2     1 12:00           b       X3        Y3      
3     2 14:05           a       X5        Y5      
4     3 16:00           c       X8        Y8      
库(dplyr)
图书馆(stringr)
图书馆(lubridate)
丰度%>%
内部联接(GPS%>%
变异(最接近的时间=提取时间(地板日期:ymd hms(str_c(Sys.date(),'GPS time',sep=''),单位为'5分钟',',
by=c(‘发生时间’=‘最近的时间’)%>%select(‘地点’=Site.x,发生时间,物种,经度,纬度)
#一个tibble:4x5
物种发生的地点时间经纬度
11:00 a X1 Y1
2 1 12:00 b X3 Y3
3 2 14:05 a X5 Y5
4 3 16:00 c X8 Y8
library(dplyr)
library(stringr)
library(lubridate)
Abundance %>% 
   inner_join(GPS %>% 
                mutate(nearest_time = str_extract(floor_date(lubridate::ymd_hms(str_c(Sys.Date(), `GPS Time`, sep = ' ')), unit = '5 mins'), '..:..:..')), 
              by = c('TimeofOccurance' = 'nearest_time')) %>% select('Site' = Site.x, TimeofOccurance, Species, Longitude, Latitude)
# A tibble: 4 x 5
   Site TimeofOccurance Species Longitude Latitude
  <dbl> <time>          <chr>   <chr>     <chr>   
1     1 11:00           a       X1        Y1      
2     1 12:00           b       X3        Y3      
3     2 14:05           a       X5        Y5      
4     3 16:00           c       X8        Y8