我正在努力"合并"如果列X和Y等于(我必须匹配dOne.X == dTwo.X & dOne.Y == dTwo.Y
和dOne.X == dTwo.Y & dOne.Y == dTwo.X
),则数据框的第五列在另一个数据框中
我使用for
循环解决了这个问题,但是当数据框dOne很大时它很慢(在我的机器中,如果length(dOne.X) == 500000
需要25分钟)。我想知道是否有办法使用更快的"矢量化"来解决这个问题。操作。以上是我想要做的事例:
Data Frame ONE
X Y V
a b 2
a c 3
a d 0
a e 0
b c 2
b d 3
b e 0
c d 2
c e 0
d e 0
Data Frame TWO
X Y V
a b 1
a c 1
a d 1
b c 1
b d 1
c d 1
e d 1
Expected Data Frame after the columns are merged
X Y V V2
a b 2 1
a c 3 1
a d 0 1
a e 0 0
b c 2 1
b d 3 1
b e 0 0
c d 2 1
c e 0 0
d e 0 1
这是我目前使用的代码,当dOne很大(数十万或几行)时,它很慢:
copyadjlistValueColumn <- function(dOne, dTwo) {
dOne$V2 <- 0
lv <- union(levels(dOne$Y), levels(dOne$X))
dTwo$X <- factor(dTwo$X, levels = lv)
dTwo$Y <- factor(dTwo$Y, levels = lv)
dOne$X <- factor(dOne$X, levels = lv)
dOne$Y <- factor(dOne$Y, levels = lv)
for(i in 1:nrow(dTwo)) {
row <- dTwo[i,]
dOne$V2[dOne$X == row$X & dOne$Y == row$Y] <- row$V
dOne$V2[dOne$X == row$Y & dOne$Y == row$X] <- row$V
}
dOne
}
这是一个测试案例,涵盖了我期望的内容(使用上面的数据框):
test_that("Copy V column to another Data Frame", {
dfOne <- data.frame(X=c("a", "a", "a", "a", "b", "b", "b", "c", "c", "d"),
Y=c("b", "c", "d", "e", "c", "d", "e", "d", "e", "e"),
V=c(2, 3, 0, 0, 2, 3, 0, 2, 0, 0))
dfTwo <- data.frame(X=c("a", "a", "a", "b", "b", "c", "e"),
Y=c("b", "c", "d", "c", "d", "d", "d"),
V=c(1, 1, 1, 1, 1, 1, 1))
lv <- union(levels(dfTwo$Y), levels(dfTwo$X))
dfExpected <- data.frame(X=c("a", "a", "a", "a", "b", "b", "b", "c", "c", "d"),
Y=c("b", "c", "d", "e", "c", "d", "e", "d", "e", "e"),
V=c(2, 3, 0, 0, 2, 3, 0, 2, 0, 0),
V2=c(1, 1, 1, 0, 1, 1, 0, 1, 0, 1))
dfExpected$X <- factor(dfExpected$X, levels = lv)
dfExpected$Y <- factor(dfExpected$Y, levels = lv)
dfMerged <- copyadjlistValueColumn(dfOne, dfTwo)
expect_identical(dfMerged, dfExpected)
})
有什么建议吗?
非常感谢:)
答案 0 :(得分:2)
尝试两个merge
,其中匹配列的顺序在第二个中反转,以获得“双向”匹配。然后你可以使用例如rowSums
将两个已创建的列折叠为一个。
d1 <- merge(dfOne, dfTwo, by.x = c("X", "Y"), by.y = c("X", "Y"), all.x = TRUE)
d2 <- merge(d1, dfTwo, by.x = c("X", "Y"), by.y = c("Y", "X"), all.x = TRUE)
cbind(dfOne, V2 = rowSums(cbind(d2$V.y, d2$V), na.rm = TRUE))
# X Y V V2
# 1 a b 2 1
# 2 a c 3 1
# 3 a d 0 1
# 4 a e 0 0
# 5 b c 2 1
# 6 b d 3 1
# 7 b e 0 0
# 8 c d 2 1
# 9 c e 0 0
# 10 d e 0 1
要更快地替换merge
,请在此处检查data.table
和dplyr
替代方案:stackoverflow.com/questions/1299871/how-to-join-data-frames-in-r-内外 - 左 - 右/
答案 1 :(得分:2)
这是一种可能的data.table
套餐方法。对于像您这样的大数据集,这种方法应该特别有效:
首先转换为data.table
对象并添加键
library(data.table)
setkey(setDT(dfOne), X, Y)
setkey(setDT(dfTwo), X, Y)
然后在X & Y
组合上执行联接 - 通过将X,Y
的关键列dfOne
与X,Y
的关键列dfTwo
分别匹配来执行联接。
dfOne[dfTwo, V2 := i.V]
现在在Y & X
组合上执行联接 - 通过将X,Y
的关键列dfOne
与Y,X
的关键列dfTwo
分别匹配来执行联接。
setkey(dfTwo, Y, X)
dfOne[dfTwo, V2 := i.V][]
结果(我将保持不匹配的NA
而不是零,因为这样更有意义):
# X Y V V2
# 1: a b 2 1
# 2: a c 3 1
# 3: a d 0 1
# 4: a e 0 NA
# 5: b c 2 1
# 6: b d 3 1
# 7: b e 0 NA
# 8: c d 2 1
# 9: c e 0 NA
# 10: d e 0 1
答案 2 :(得分:2)
使用dplyr
:
library(dplyr)
left_join(dfOne, dfTwo, by = c("X", "Y")) %>%
left_join(dfTwo, by = c("X" = "Y", "Y" = "X")) %>%
mutate(V2 = ifelse(is.na(V.y), V, V.y)) %>%
select(X, Y, V = V.x, V2) %>%
do(replace(., is.na(.), 0))