R - merge 2 dataframes with timestamp between

 ̄綄美尐妖づ 提交于 2021-02-10 14:41:03

问题


DF1:

structure(list(X = c(113.8577674, 113.8577537, 113.8577403), 
    Y = c(22.19537297, 22.19537222, 22.1953723), Date = c("7/1/2016", 
    "7/1/2016", "7/1/2016"), Time = structure(c(9474, 9484, 9494
    ), class = c("hms", "difftime"), units = "secs"), TrackTime = structure(c(38274, 
    38284, 38294), class = c("hms", "difftime"), units = "secs")), .Names = c("X", 
"Y", "Date", "Time", "TrackTime"), row.names = c(NA, -3L), class = "data.frame", spec = structure(list(
    cols = structure(list(X = structure(list(), class = c("collector_double", 
    "collector")), Y = structure(list(), class = c("collector_double", 
    "collector")), Date = structure(list(), class = c("collector_character", 
    "collector")), Time = structure(list(format = ""), .Names = "format", class = c("collector_time", 
    "collector")), TrackTime = structure(list(format = ""), .Names = "format", class = c("collector_time", 
    "collector"))), .Names = c("X", "Y", "Date", "Time", "TrackTime"
    )), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

DF2:

structure(list(DATE_SIGHT = "7-Jan-16", GROUP_SIGHT = 1L, TIME_START = structure(38280, class = c("hms", 
"difftime"), units = "secs"), TIME_END = structure(39060, class = c("hms", 
"difftime"), units = "secs"), GRP_SIZE = 1L), .Names = c("DATE_SIGHT", 
"GROUP_SIGHT", "TIME_START", "TIME_END", "GRP_SIZE"), row.names = c(NA, 
-1L), class = "data.frame", spec = structure(list(cols = structure(list(
    DATE_SIGHT = structure(list(), class = c("collector_character", 
    "collector")), GROUP_SIGHT = structure(list(), class = c("collector_integer", 
    "collector")), TIME_START = structure(list(format = ""), .Names = "format", class = c("collector_time", 
    "collector")), TIME_END = structure(list(format = ""), .Names = "format", class = c("collector_time", 
    "collector")), GRP_SIZE = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("DATE_SIGHT", "GROUP_SIGHT", "TIME_START", 
"TIME_END", "GRP_SIZE")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

DF1 look like this:

         X        Y     Date     Time TrackTime
1 113.8578 22.19537 7/1/2016 02:37:54  10:37:54
2 113.8578 22.19537 7/1/2016 02:38:04  10:38:04
3 113.8577 22.19537 7/1/2016 02:38:14  10:38:14

DF2 look like this:

  DATE_SIGHT GROUP_SIGHT TIME_START TIME_END GRP_SIZE
1   7-Jan-16           1   10:38:00 10:51:00        1

I would like to match if DF1's row (TrackTime) lies within the DF2's TIME_START AND TIME_END. Then keep only those lies within, and also associate the other column to DF1. So the final product would look like this:

         X        Y     Date     Time TrackTime GROUP_SIGHT GRP_SIZE
1 113.8578 22.19537 7/1/2016 02:38:04  10:38:04           1        1
2 113.8577 22.19537 7/1/2016 02:38:14  10:38:14           1        1

回答1:


data.table's foverlaps() is my preferred weapon of choice here. But first, you have to create some correct (POSIXct) timestamps to join on..

library( data.table )
#create two data.tables
dt1 <- as.data.table( DF1 )
dt2 <- as.data.table( DF2 )
#add suffixes to columns, to identify them after the join
names( dt1 ) <- paste0( names( dt1 ), ".dt1" )
names( dt2 ) <- paste0( names( dt2 ), ".dt2" )
#save column order for later
colorder <- c( names( dt1 ), names( dt2 ) )
#set date-time as poosix-stamps
dt1[, `:=`( start.join = as.POSIXct( paste0( Date.dt1, TrackTime.dt1 ), format = "%d/%m/%Y%H:%M:%S" ),
            end.join = as.POSIXct( paste0( Date.dt1, TrackTime.dt1 ), format = "%d/%m/%Y%H:%M:%S" ) )]
dt2[, `:=`( start.join = as.POSIXct( paste0( DATE_SIGHT.dt2, TIME_START.dt2 ), format = "%d-%b-%y%H:%M:%S" ),
            end.join = as.POSIXct( paste0( DATE_SIGHT.dt2, TIME_END.dt2 ), format = "%d-%b-%y%H:%M:%S" ) )]
#set key on dt2
setkey( dt2, start.join, end.join )
#perform the overlap join
result <- foverlaps( dt1, dt2, type = "within", nomatch = 0L )
#drop the join columns
result[, grep( ".join$", names( result ) ) := NULL]
#set the order of columns right
setcolorder( result, colorder )

#       X.dt1    Y.dt1 Date.dt1 Time.dt1 TrackTime.dt1 DATE_SIGHT.dt2 GROUP_SIGHT.dt2 TIME_START.dt2 TIME_END.dt2 GRP_SIZE.dt2
# 1: 113.8578 22.19537 7/1/2016 02:38:04      10:38:04       7-Jan-16               1       10:38:00     10:51:00            1
# 2: 113.8577 22.19537 7/1/2016 02:38:14      10:38:14       7-Jan-16               1       10:38:00     10:51:00            1


来源:https://stackoverflow.com/questions/53694729/r-merge-2-dataframes-with-timestamp-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!