14

我有两个这样的数据框:

set.seed(1)
df <- cbind(expand.grid(x=1:3, y=1:5), time=round(runif(15)*30))
to.merge <- data.frame(x=c(2, 2, 2, 3, 2),
                       y=c(1, 1, 1, 5, 4),
                       time=c(17, 12, 11.6, 22.5, 2),
                       val=letters[1:5],
                       stringsAsFactors=F)

我想合并to.mergedf(with all.x=T) 中,这样:

  • df$x == to.merge$x
  • df$y == to.merge$y
  • abs(df$time - to.merge$time) <= 1; 在满足多个的情况下to.merge,我们选择最小化这个距离的那个。

我怎样才能做到这一点?

所以我想要的结果是(这只是为匹配行添加df了相应value的列to.merge):

   x y time val
1  1 1    8  NA
2  2 1   11   c
3  3 1   17  NA
4  1 2   27  NA
5  2 2    6  NA
6  3 2   27  NA
7  1 3   28  NA
8  2 3   20  NA
9  3 3   19  NA
10 1 4    2  NA
11 2 4    6  NA
12 3 4    5  NA
13 1 5   21  NA
14 2 5   12  NA
15 3 5   23   d

在哪里to.merge

  x y time val
1 2 1 17.0   a
2 2 1 12.0   b
3 2 1 11.6   c
4 3 5 22.5   d
5 2 4  2.0   e

注意 - (2, 1, 17, a) 不匹配,df因为对于 (X, Y) = (2, 1),time17 与 11 的距离大于 1 。df$time

此外,有两行to.merge满足匹配df's (2, 1, 11) 行的条件,但是选择了 'c' 行而不是 'b' 行,因为它time最接近 11。

最后,可能有一些行to.mergedf.


一种可行的方法是 for 循环,但我的数据需要很长时间(df有 ~12k 行和to.merge~250k 行)

df$value <- NA
for (i in 1:nrow(df)) {
    row <- df[i, ]
    idx <- which(row$x == to.merge$x &
                 row$y == to.merge$y &
                 abs(row$time - to.merge$time) <= 1)
    if (length(idx)) {
        j <- idx[which.min(row$time - to.merge$time[idx])]
        df$val[i] <- to.merge$val[j]
    }
}

我觉得我可以以某种方式进行合并,例如:

to.merge$closest_time_in_df <- sapply(to.merge$time,
                                  function (tm) {
                                     dts <- abs(tm - df$time)
                                     # difference must be at most 1
                                     if (min(dts) <= 1) {
                                         df$time[which.min(dts)]
                                     } else {
                                         NA
                                     }
                                  })
merge(df, to.merge,
      by.x=c('x', 'y', 'time'),
      by.y=c('x', 'y', 'closest_time_in_df'),
      all.x=T)

但这不会合并该(2, 1, 11)行,因为to.merge$closest_time_in_dffor(2, 1, 11.5, c)是 12,但是 12 in 的时间df对应于 (x, y) = (2, 5) 而不是 (2, 1) 因此合并失败。

4

3 回答 3

15

使用data.tableandroll='nearest'或 限制为 1,roll = 1, rollends = c(TRUE,TRUE)

例如

library(data.table)
# create data.tables with the same key columns (x, y, time)
DT <- data.table(df, key = names(df))
tm <- data.table(to.merge, key = key(DT))

# use join syntax with roll = 'nearest'


tm[DT, roll='nearest']

#     x y time val
#  1: 1 1    8  NA
#  2: 1 2   27  NA
#  3: 1 3   28  NA
#  4: 1 4    2  NA
#  5: 1 5   21  NA
#  6: 2 1   11   c
#  7: 2 2    6  NA
#  8: 2 3   20  NA
#  9: 2 4    6   e
# 10: 2 5   12  NA
# 11: 3 1   17  NA
# 12: 3 2   27  NA
# 13: 3 3   19  NA
# 14: 3 4    5  NA
# 15: 3 5   23   d

您可以通过设置roll=-1rollends = c(TRUE,TRUE)

new <- tm[DT, roll=-1, rollends  =c(TRUE,TRUE)]
new
    x y time val
 1: 1 1    8  NA
 2: 1 2   27  NA
 3: 1 3   28  NA
 4: 1 4    2  NA
 5: 1 5   21  NA
 6: 2 1   11   c
 7: 2 2    6  NA
 8: 2 3   20  NA
 9: 2 4    6  NA
10: 2 5   12  NA
11: 3 1   17  NA
12: 3 2   27  NA
13: 3 3   19  NA
14: 3 4    5  NA
15: 3 5   23   d

或者你可以先roll=1,然后roll=-1,然后合并结果(整理来自第二个rolling join的val.1列)

new <- tm[DT, roll = 1][tm[DT,roll=-1]][is.na(val), val := ifelse(is.na(val.1),val,val.1)][,val.1 := NULL]
new
    x y time val
 1: 1 1    8  NA
 2: 1 2   27  NA
 3: 1 3   28  NA
 4: 1 4    2  NA
 5: 1 5   21  NA
 6: 2 1   11   c
 7: 2 2    6  NA
 8: 2 3   20  NA
 9: 2 4    6  NA
10: 2 5   12  NA
11: 3 1   17  NA
12: 3 2   27  NA
13: 3 3   19  NA
14: 3 4    5  NA
15: 3 5   23   d
于 2013-04-19T01:54:01.227 回答
6

使用merge几次aggregate一次,这里是如何做到的。

set.seed(1)
df <- cbind(expand.grid(x = 1:3, y = 1:5), time = round(runif(15) * 30))
to.merge <- data.frame(x = c(2, 2, 2, 3, 2), y = c(1, 1, 1, 5, 4), time = c(17, 12, 11.6, 22.5, 2), val = letters[1:5], stringsAsFactors = F)

#Find rows that match by x and y
res <- merge(to.merge, df, by = c("x", "y"), all.x = TRUE)
res$dif <- abs(res$time.x - res$time.y)
res
##   x y time.x val time.y dif
## 1 2 1   17.0   a     11 6.0
## 2 2 1   12.0   b     11 1.0
## 3 2 1   11.6   c     11 0.6
## 4 2 4    2.0   e      6 4.0
## 5 3 5   22.5   d     23 0.5

#Find rows that need to be merged
res1 <- merge(aggregate(dif ~ x + y, data = res, FUN = min), res)
res1
##   x y dif time.x val time.y
## 1 2 1 0.6   11.6   c     11
## 2 2 4 4.0    2.0   e      6
## 3 3 5 0.5   22.5   d     23

#Finally merge the result back into df
final <- merge(df, res1[res1$dif <= 1, c("x", "y", "val")], all.x = TRUE)
final
##    x y time  val
## 1  1 1    8 <NA>
## 2  1 2   27 <NA>
## 3  1 3   28 <NA>
## 4  1 4    2 <NA>
## 5  1 5   21 <NA>
## 6  2 1   11    c
## 7  2 2    6 <NA>
## 8  2 3   20 <NA>
## 9  2 4    6 <NA>
## 10 2 5   12 <NA>
## 11 3 1   17 <NA>
## 12 3 2   27 <NA>
## 13 3 3   19 <NA>
## 14 3 4    5 <NA>
## 15 3 5   23    d
于 2013-04-19T02:26:34.603 回答
0

mnel 的答案roll = "nearest"在连接中使用,data.table但不限于 OP 要求的 +/- 1。此外,MichaelChirico 建议使用该on参数。

这种方法使用

  • roll = "nearest",
  • 通过引用更新,即不复制,
  • setDT()强制 data.framedata.table不复制(2014-02-27 引入 v.1.9.2 of data.table),
  • 用于显式设置密钥的on参数(在 2015-09-19 与 v.1.9.6 中引入)。

所以,下面的代码

library(data.table)   # version 1.11.4 used
setDT(df)[setDT(to.merge), on  = .(x, y, time), roll = "nearest",
          val := replace(val, abs(x.time - i.time) > 1, NA)]
df

已更新df

    x y time  val
 1: 1 1    8 <NA>
 2: 2 1   11    c
 3: 3 1   17 <NA>
 4: 1 2   27 <NA>
 5: 2 2    6 <NA>
 6: 3 2   27 <NA>
 7: 1 3   28 <NA>
 8: 2 3   20 <NA>
 9: 3 3   19 <NA>
10: 1 4    2 <NA>
11: 2 4    6 <NA>
12: 3 4    5 <NA>
13: 1 5   21 <NA>
14: 2 5   12 <NA>
15: 3 5   23    d

请注意,行的顺序没有改变(与钦梅帕蒂尔的回答相反)

如果df不能更改,可以创建一个新的 data.table

result <- setDT(to.merge)[setDT(df), on  = .(x, y, time), roll = "nearest",
                .(x, y, time, val = replace(val, abs(x.time - i.time) > 1, NA))]
result

它返回与上面相同的结果。

于 2018-07-19T12:05:15.867 回答