Count occurence of values for every possible pair

后端 未结 3 824
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 04:59

I have a list of ids and places where these ids have been. Now I want to find pairs of ids that have most places in common.

My data frame looks like this:



        
3条回答
  •  时光说笑
    2021-01-22 05:23

    For a dplyr-esque solution,

    You could do:

    left_join(df, df, by = "place") %>%
      rename(pair1 = id.x, pair2 = id.y) %>%
      filter(!pair1 == pair2, !duplicated(t(apply(., 1, sort))) == TRUE) %>% 
      count(pair1, pair2) 
    

提交回复
热议问题