Count occurence of values for every possible pair

后端 未结 3 826
隐瞒了意图╮
隐瞒了意图╮ 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:06

    Or...

    library(dplyr)
    
    df1 %>%
      left_join(df1, by = "place") %>%
      filter(id.x < id.y) %>%
      group_by(id.x, id.y) %>%
      summarise(count = n())
    

    EDIT: If IDs are factors operator < won't work. Conversion adds another line to the solution (credits to Steven Beaupré):

    df1 %>%
      left_join(df1, by = "place") %>%
      mutate_each(funs(as.character(.))) %>%
      filter(id.x < id.y) %>%
      group_by(id.x, id.y) %>%
      summarise(count = n())
    

提交回复
热议问题