Subset with unique cases, based on multiple columns

前端 未结 7 2145
执笔经年
执笔经年 2020-12-04 12:06

I\'d like to subset a dataframe to include only rows that have unique combinations of three columns. My situation is similar to the one presented in this question, but I\'d

7条回答
  •  天命终不由人
    2020-12-04 12:52

    Using dplyr you could do:

    library(dplyr)
    
    # distinct
    df %>% 
      distinct(v1, v2, v3, .keep_all = T)
    
    # non-distinct only
    df %>% 
      group_by(v1, v2, v3) %>% 
      filter(n() > 1)
    
    # exclude any non-distinct
    df %>% 
      group_by(v1, v2, v3) %>% 
      filter(n() == 1)
    

提交回复
热议问题