How to compare two list columns to find common elements?

dplyr
purrr

#1

I have two nested list columns and want to find the elements which are in both.

library(tidyverse)

df <-data.frame(stringsAsFactors=FALSE,
     session = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
        user = c("A", "B", "C", "D", "E", "A", "B", "C", "X", "Y", "R", "S",
                 "T", "D", "E"))

df <- df %>% 
  group_by(session) %>% 
  nest() %>% 
  mutate(session.before=lag(data))

Unfortunately, my purrr expertise is only slowly growing...
This doesn't work:

df %>%  
  mutate(users.before=map2(data, session.before, inner_join))

Is it something with purrr::keep()?

Greateful for any hint! Many Thanks!


#2

ok. this does the trick

df <- df %>%  
  mutate(users.before=map2(data, session.before, safely(intersect)))%>%  
  mutate(users.before=map(users.before, "result"))

if there is a more elegant version etc, still grateful for any hint/comment


#3

the issue is that lag creates empty session.before. When you are running map2, it fails because of that. So another option is to drop rows where session.before is empty and then you can use your solution from before.


closed #4

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.