Unique on a dataframe with only selected columns

前端 未结 4 1155
逝去的感伤
逝去的感伤 2020-11-27 13:13

I have a dataframe with >100 columns, and I would to find the unique rows, by comparing only two of the columns. I\'m hoping this is an easy one, but I can\'t get it working

4条回答
  •  甜味超标
    2020-11-27 13:48

    Here are a couple dplyr options that keep non-duplicate rows based on columns id and id2:

    library(dplyr)                                        
    df %>% distinct(id, id2, .keep_all = TRUE)
    df %>% group_by(id, id2) %>% filter(row_number() == 1)
    df %>% group_by(id, id2) %>% slice(1)
    

提交回复
热议问题