Select rows with common ids in grouped data frame

前端 未结 2 566
自闭症患者
自闭症患者 2021-01-15 23:22

I am searching for a simpler solution to the following problem. Here is my setup:

test <- tibble::tribble(
  ~group_name, ~id_name, ~varA, ~varB,
     \"g         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-15 23:48

    In base R, we can split id_name by group_name find common id's and then subset

    subset(test, id_name %in% Reduce(intersect, split(id_name, group_name)))
    
    #   group_name id_name  varA varB 
    #             
    # 1 groupA     id_1        1 a    
    # 2 groupA     id_2        4 f    
    # 3 groupA     id_4        6 x    
    # 4 groupA     id_4        6 h    
    # 5 groupB     id_1        2 s    
    # 6 groupB     id_2       13 y    
    # 7 groupB     id_4       14 t    
    # 8 groupC     id_1        3 d    
    # 9 groupC     id_2        7 j    
    #10 groupC     id_4        9 l    
    

    Using similar concept in tidyverse, it would be

    library(tidyverse)
    test %>%
      filter(id_name %in% (test %>%
                             group_split(group_name)  %>%
                             map(~pull(., id_name)) %>%
                             reduce(intersect)))
    

提交回复
热议问题