Select rows with common ids in grouped data frame

前端 未结 2 1282
臣服心动
臣服心动 2021-01-15 23:01

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

    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,]
    

提交回复
热议问题