R:通过* list *列连接两个表(小标题) [英] R: Join two tables (tibbles) by *list* columns
问题描述
似乎应该对此有一个简单的答案,但我却找不到一个答案:
Seems like there should be a simple answer for this but I haven't been able to find one:
tib1 <- tibble(x = list(1, 2, 3), y = list(4, 5, 6))
tib1
# A tibble: 3 × 2
x y
<list> <list>
1 <dbl [1]> <dbl [1]>
2 <dbl [1]> <dbl [1]>
3 <dbl [1]> <dbl [1]>
tib2 <- tibble(x = list(1, 2, 4, 5), y = list(4, c(5, 10), 6, 7))
tib2
# A tibble: 4 × 2
x y
<list> <list>
1 <dbl [1]> <dbl [1]>
2 <dbl [1]> <dbl [2]>
3 <dbl [1]> <dbl [1]>
4 <dbl [1]> <dbl [1]>
dplyr::inner_join(tib1, tib2)
加入= = c("x","y")
Joining, by = c("x", "y")
inner_join_impl(x,y,by $ x,by $ y,后缀$ x,后缀$ y)中的错误: 由于类型(列表/列表)不兼容,无法加入'x'x'x'
Error in inner_join_impl(x, y, by$x, by$y, suffix$x, suffix$y) : Can't join on 'x' x 'x' because of incompatible types (list / list)
在我开始编写自己的列之前,有没有一种方法可以基于 list 列执行联接?
So is there a way to perform a join based on list columns (before I start writing my own)?
基本上,如果两个关键变量的列表相同,则我希望该行包含在最终表中,如果不行,则不包含.在上面的示例中,有两个关键变量x
和y
,结果应该仅是两个tibble
中的第一行,因为它是两个关键变量中唯一相同的变量:
Basically if the list of both key variables is identical, I want the row to be included in the final table, and if not - not. In the above example there are two key variables x
and y
and the result should be only the first row in the two tibble
s since it's the only identical one in both key variables:
tibble(x = list(1), y = list(4))
# A tibble: 1 × 2
x y
<list> <list>
1 <dbl [1]> <dbl [1]>
推荐答案
我们可以使用digest
中的哈希:
We could use hashes from digest
:
tib1 <- tibble(x = list(1, 2, 3), y = list(4, 5, 6))
tib2 <- tibble(x = list(1, 2, 4, 5), y = list(4, c(5, 10), 6, 7))
tib1 <- mutate_all(tib1, funs(hash = map_chr(., digest::digest)))
tib2 <- mutate_all(tib2, funs(hash = map_chr(., digest::digest)))
inner_join(tib1, tib2, c('x_hash', 'y_hash')) %>%
select(x.x, x.y)
# A tibble: 1 × 2
x.x x.y
<list> <list>
1 <dbl [1]> <dbl [1]>
这篇关于R:通过* list *列连接两个表(小标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!