Reputation: 37
Suppose I have two tibbles: a
and b
.
a <- tibble("unordered_characters" = list( c("A","B"), c("X","Y") ) ,
"v" = c("G", "F"))
b <- tibble("unordered_characters" = list( c("B","A"), c("Y","X")),
"x" = c("M", "d"))
I want to join b
on a
using their common variable, which is a list. But I want the list to be unordered, so that c("A","B")
should be the same as c("B","A")
.
In other words, setequal(c("A","B"),c("B","A"))
returns TRUE
. This would to create the following table:
unordered_characters v x
------------------------
c("A","B") G M
c("X","Y") F d
But a %>% left_join(b)
results in the following:
unordered_characters v x
------------------------
c("A","B") G NA
c("X","Y") F NA
How can I fix this?
Upvotes: 2
Views: 38
Reputation: 887118
We could use map
to loop over the list
and sort
before doing the join
library(dplyr)
library(purrr)
a %>%
mutate(unordered_characters = map(unordered_characters, sort)) %>%
left_join(b %>%
mutate(unordered_characters = map(unordered_characters, sort)))
# A tibble: 2 x 3
# unordered_characters v x
# <list> <chr> <chr>
#1 <chr [2]> G M
#2 <chr [2]> F d
Upvotes: 1