Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

R: Join two tables (tibbles) by *list* columns

$
0
0

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)

Joining, by = c("x", "y")

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)

So is there a way to perform a join based on list columns (before I start writing my own)?

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 tibbles 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]>

Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>