您好我有两个表(下面的table1和table2),并希望根据最接近的时间戳加入它们以形成expected_output。如果可能的话,涉及dplyr的某种解决方案会很好,但如果它进一步使事情变得复杂则不会。
table1 =
structure(list(date = structure(c(1437051300, 1434773700, 1431457200
), class = c("POSIXct", "POSIXt"), tzone = ""), val1 = c(94L,
33L, 53L)), .Names = c("date", "val1"), row.names = c(NA, -3L
), class = "data.frame")
table2 =
structure(list(date = structure(c(1430248288, 1435690482, 1434050843
), class = c("POSIXct", "POSIXt"), tzone = ""), val2 = c(67L,
90L, 18L)), .Names = c("date", "val2"), row.names = c(NA, -3L
), class = "data.frame")
expected_output =
structure(list(date = structure(c(1437051300, 1434773700, 1431457200
), class = c("POSIXct", "POSIXt"), tzone = ""), val1 = c(94L,
33L, 53L), val2 = c(90L, 18L, 67L)), .Names = c("date", "val1",
"val2"), row.names = c(NA, -3L), class = "data.frame")
答案 0 :(得分:14)
使用data.table
与roll = "nearest"
的滚动连接功能:
require(data.table) # v1.9.6+
setDT(table1)[, val2 := setDT(table2)[table1, val2, on = "date", roll = "nearest"]]
此处,val2
列是通过使用date
选项对roll = "nearest"
列执行加入来创建的。对于table1$date
的每一行,计算table2$date
中距离最近的匹配行,并提取相应行的val2
。
答案 1 :(得分:2)
这可能会很慢,但是......
d <- function(x,y) abs(x-y) # define the distance function
idx <- sapply( table1$date, function(x) which.min( d(x,table2$date) )) # find matches
cbind(table1,table2[idx,-1,drop=FALSE])
# date val1 val2
# 2 2015-07-16 08:55:00 94 90
# 3 2015-06-20 00:15:00 33 18
# 1 2015-05-12 15:00:00 53 67
构建idx
的另一种方法是max.col(-outer(table1$date, table2$date, d))
。