Select rows with common ids in grouped data frame

前端 未结 2 549
自闭症患者
自闭症患者 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:33

    You can tabulate the the occurrence of id_name by group_name:

    table(test$group_name,test$id_name)
    

    If id_name is present in every group, we want columns that have all > 0 entris. We can simplify this logic using a combination of >0 and colMeans:

    keep = names(which(colMeans(table(test$group_name,test$id_name)>0)==1))
    

    Using this:

    test[test$id_name %in% keep,]
    

提交回复
热议问题